How to tell programmatically if object exists

  • Thread starter Thread starter Steven
  • Start date Start date
S

Steven

I want to delete a table in a database if it exists but I
want to ask first if the table exists so it will not error
from trying to delete a nonexisting object.

How do I ask if the table object exists first.

Thank you for your help.

Steven
 
You can loop through the collection to see whether a table with that name
exists:

Function TableExists(TableName as String) As Boolean
' Returns True if TableName exists, False otherwise

Dim dbCurr As Database
Dim tdfCurr As TableDef

TableExists = False
Set dbCurr = CurrentDb()
For Each tdfCurr In dbCurr.TableDefs
If StrComp(tdfCurr.Name, TableName, vbTextCompare) = 0 Then
TableExists = True
Exit For
End If
Next tdfCurr

End Function

(Note that the above uses DAO. If you're using Access 2000 or newer, you'll
need to ensure that you set a reference to DAO)

However, the easier way is simply to put On Error Resume Next in front of
the delete statement, and not worry about whether or not an error occurs
when you try to do the delete. The end result is going to be the same: you
won't have the table.
 
Back
Top