How do I find if a table is in the current DB

F

Fred Wilson

Hello,

I am trying to use something like

If Currentdb.tabledef.Name = strTableName then
blTableHere = True
else
blTableHere = False
End if

Something is not working right. I am sure that I am not using the
correct form. Can you help me out?

Thanks,
Fred
 
D

Dirk Goldgar

Fred Wilson said:
Hello,

I am trying to use something like

If Currentdb.tabledef.Name = strTableName then
blTableHere = True
else
blTableHere = False
End if

Something is not working right. I am sure that I am not using the
correct form. Can you help me out?

Thanks,
Fred

There are several approaches to solving this problem. Here's a function
you can call that uses one of them:

'----- start of code -----
Function fncTableExists( _
TableName As String, _
Optional DBPath As String) _
As Boolean

Dim db As DAO.Database

If Len(DBPath) > 0 Then
Set db = DBEngine.OpenDatabase(DBPath)
Else
Set db = CurrentDb
End If

If Len(TableName) = 0 Then Err.Raise 5

On Error Resume Next
fncTableExists = IsObject(db.TableDefs(TableName))

If Not db Is Nothing Then
db.Close
Set db = Nothing
End If

End Function
'----- end of code -----

You'd call the function (to see if a table exists in the current
database) like this:

If fncTableExists(strTableName) Then
' ... the table exists ...
Else
' ... it doesn't ...
End If

The function code requires that you have a reference set to the
Microsoft DAO Object Library. If you're using Access 2000 or 2002, you
may have to add the reference to the DAO 3.6 library yourself, using the
VB Editor's Tools -> References... dialog.
 
F

Fred Wilson

Hmmm. I will check that out. I am using Access 2000 so I will set the
reference.

Thanks,
Fred
 
Top