Checking If Table Exists

C

Craig

Is it possible to use VBA code to see if a table already
exists before creating a table? If so, what would the
syntax for that be? Thanks...

Craig
 
G

Graeme Richardson

Hi Craig, this function is one way to do it.

Public Function tableExists(ByVal vstrTableName As String) As Boolean
' Requires reference to DAO
On Error Resume Next

Dim db As DAO.Database: Set db = DBEngine.Workspaces(0).Databases(0)
Dim tdf As DAO.TableDef

db.TableDefs.Refresh
Set tdf = db.TableDefs(vstrTableName)

tableExists = (Err.Number = 0) ' Error generated if table not found

End Function
 
T

Tim Ferguson

Is it possible to use VBA code to see if a table already
exists before creating a table? If so, what would the
syntax for that be?


' catch the error, just in case
On Error Resume Next

' Now try to drop the table
db.Execute "DROP TABLE OldTable", dbFailOnError


' actually it's probably safe to ignore the error
' but this is how you would respond to it
If Err.Number <> 0 Then
MsgBox "Don't worry it wasn't there!"

End If

' reset normal error handling
On Error Goto 0


Hope that helps


Tim F
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Suppress CREATE TABLE 1
DLookup 8
Checking if table exists 1
Deleting Tables 4
Find exact Match 2
how to delete a table if it exists 2
Deleting tables 9
How to export table from outlook into ms access table? 0

Top