Checking if table exists

C

Craig

I want to check and see if a table exists before I start
manipulating the data in it with VBA code. I have the
following...

Function TableExists(ByVal vstrTableName As String) As
Boolean

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) <<Item not found in
collection

End Function



In the main code, I am passing the name of the table into
the function call. It kees telling me that item is not
found in collection. Any way to correct this?

Thanks...

Craig
 
W

Wayne Morgan

A couple of options, accept the error and trap it in an error handler or
loop through the TableDefs collection to see if the table exists.

For Each tdf in db.TableDefs
If tdf.Name = vstrTableName Then
'Table exists, exit loop.
bolTableExists = True
Exit For
End If
'Table doesn't exist, do whatever here
bolTableExists = False
Next
 

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


Top