开发者

How do you overwrite a table created in VBA?

开发者 https://www.devze.com 2023-03-22 06:07 出处:网络
I\'m creating a table in VBA within a loop and when I run the code a table is created. But the next time I run it, an error comes up telling me that the table exists and the remainder of the code is n

I'm creating a table in VBA within a loop and when I run the code a table is created. But the next time I run it, an error comes up telling me that the table exists and the remainder of the code is not executed. How can I have it overwrite the existing table (from the previous run)?

Here is my code:

Option Compare Database

Public Function createTa开发者_如何学JAVAbles()

    Dim db As DAO.Database
    Dim tdf As DAO.TableDef
    Dim rst As DAO.Recordset
    Dim fld As DAO.Field
    Dim strSQL As String

    strSQL = "Select SKUS from SKUS"
    Set db = CurrentDb()
    Set rst = db.OpenRecordset(strSQL)
    Set fld = rst.Fields("SKUS")

    'MsgBox fld.Value

    rst.MoveFirst
    Do While Not rst.EOF
        Set tdf = db.CreateTableDef(fld.Value)

        Set fld = tdf.CreateField("SKUS", dbText, 30)
        tdf.Fields.Append fld

        Set fld = tdf.CreateField("Count", dbInteger)
        tdf.Fields.Append fld

        db.TableDefs.Append tdf

        rst.MoveNext
    Loop

End Function

Can anyone help me out please? Thanks!


I would simply delete the table before attempting to recreate it:

db.TableDefs.Delete fld.Value


You can check if the table exists with the following function.

Public Function TableExists(TabName As String) As Boolean

    Dim db As DAO.Database
    Dim Sdummy As String

    Set db = CurrentDb()

    On Error Resume Next
    Sdummy = db.TableDefs(TabName).Name
    TableExists = (Err.Number = 0)

End Function

If the function returns true, then issue following sql statement:

DROP TABLE SKUS


The usual method is to test then delete temp table, requiring more code and recordkeeping for calling procedures that run multiple maketables.

Here is a procedure that is all inclusive, gleaning the source table name from maketable, then deleting before recreating. Also returns number of new records.

Public Function fcnMakeTableForce(strMTQuery As String) As Integer
On Error GoTo ErrorExit

'Runs maketable, deleting the resulting temp table contained in the query (if it 
'exists) beforehand. Also returns the number of records in new temp table   

Dim dbs As Database
Dim strSQL As String
Set dbs = CurrentDb

'Get SQL from MakeTable
strSQL = dbs.QueryDefs(strMTQuery).sql

'Get target table from SQL:
intINTOPos = InStr(strSQL, "INTO [") + 5
intFROMPos = InStr(strSQL, "FROM [") - 3
strTargetTable = Mid(strSQL, intINTOPos + 1, intFROMPos - intINTOPos - 1)

'Clear target table if it exists
If (DCount("*", "MSysObjects", "[Name] = """ & strTargetTable & """")) > 0 Then 
   CurrentDb.TableDefs.Delete (strTargetTable) 
End If

dbs.Execute strMTQuery
intRecordsAdded = DCount("*", strTargetTable)
fcnMakeTableForce = intRecordsAdded

NormalExit:
   Exit Function
ErrorExit:
   MsgBox "Error: " & Err.Description & vbCr & vbCr & "in Function: fcnMakeTableForce"
   Resume NormalExit

End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消