SELECT *
INTO Table2
FROM [C:\Test\Test.accdb].Table1;
Public Function TblExists(ByVal strTableName As String) As Boolean
Dim tblN As Object
TblExists = False
For Each tblN In CurrentDb.TableDefs
If tblN.Name = strTableName Then
TblExists = True
Exit For
End If
Next tblN
End Function
Sub tst()
If TblExists("Table2") Then
' Delete Table2
DoCmd.DeleteObject acTable, "Table2"
End If
' Copy Table1 toTable 2 in CurrentDB
DoCmd.CopyObject , "Table2", acTable, "Table1"
End Sub
Public Function TblExists(ByVal strTableName As String) As Boolean
Dim tblN As Object
TblExists = False
For Each tblN In Adb.TableDefs
If tblN.Name = strTableName Then
TblExists = True
Exit For
End If
Next tblN
End Function
Dim Adb As Object: Set Adb = CreateObject("Access.Application")
Call Adb.OpenCurrentDatabase(F1)
Adb.Visible = False
Public Function TblExists(ByVal strTableName As String) As Boolean
TblExists = False
If Not IsNull(DLookup("Name", "MSysObjects", "Name = " & Chr(34) & strTableName & Chr(34) & " AND Type = 1")) Then TblExists = True
End Function
Tables are represented by the TableDefs collection who's table name is the key.
So to check if the key is present, we can just attempt to retrieve it, if it fail, the table do not exist, and an error will be raised. We intercept it with an error handler:
Open in new window
To use it, just pass the TableDefs collection and table name arguments to the function:
Open in new window