Hi,
I'm trying to combine a couple of todays posts with "CreateQueryDef" code that I have to Create a table (if it doesn't already exist) but it is going horribly wrong. It starts of badly by not getting past the first 2 "Dim's" with error message "user-defined type not defined" and then with those 2 commented out it stops at "TableExists" with another "ambiguous name detected: TableExists" message. Could anyone please point out to me where I am going wrong and maybe offer some advise as to how to fix it. Thanks.
The Code;
Private Sub Command64_Click()
Dim db As Database
Dim tdf As TableDef
Dim strSQL As String
Set db = CurrentDb
If Not TableExists("tblPcNew") Then
Set tdf = db.CreateTableDef("tblPcNew")
Else
Set tdf = db.TableDefs("tblPcNew")
End If
strSQL = "INSERT INTO tblPcQueryNew " & _
"From tbltruststaff "
' Pass the SQL string to the query
tdf.SQL = strSQL
Set tdf = Nothing
Set db = Nothing
End Sub
and the Module:
Function TableExists(strTableName As String) As Boolean
' This procedure returns True or False depending on whether
' the table named in strTableName exists.
Dim dbs As Database, tdf As TableDef
On Error Resume Next
Set dbs = CurrentDb
Set tdf = dbs.TableDefs(strTableName)
If Err = 3265 Then
' Table does not exist.
TableExists = False
Else
' Table exists.
TableExists = True
End If
Err = 0
End Function
Tom.
I'm trying to combine a couple of todays posts with "CreateQueryDef" code that I have to Create a table (if it doesn't already exist) but it is going horribly wrong. It starts of badly by not getting past the first 2 "Dim's" with error message "user-defined type not defined" and then with those 2 commented out it stops at "TableExists" with another "ambiguous name detected: TableExists" message. Could anyone please point out to me where I am going wrong and maybe offer some advise as to how to fix it. Thanks.
The Code;
Private Sub Command64_Click()
Dim db As Database
Dim tdf As TableDef
Dim strSQL As String
Set db = CurrentDb
If Not TableExists("tblPcNew") Then
Set tdf = db.CreateTableDef("tblPcNew")
Else
Set tdf = db.TableDefs("tblPcNew")
End If
strSQL = "INSERT INTO tblPcQueryNew " & _
"From tbltruststaff "
' Pass the SQL string to the query
tdf.SQL = strSQL
Set tdf = Nothing
Set db = Nothing
End Sub
and the Module:
Function TableExists(strTableName As String) As Boolean
' This procedure returns True or False depending on whether
' the table named in strTableName exists.
Dim dbs As Database, tdf As TableDef
On Error Resume Next
Set dbs = CurrentDb
Set tdf = dbs.TableDefs(strTableName)
If Err = 3265 Then
' Table does not exist.
TableExists = False
Else
' Table exists.
TableExists = True
End If
Err = 0
End Function
Tom.