Using Macro to ask if my table exists.

J

Jim Butcher

I am writing a program that automatically generates a
table if data exists. Later in the macro I have to delete
the table if it exists, so I have to check whether it
exists or not. The problem I have is when I check to see
if the table exists the function DoesObjectExist()works
correctly the first time but does not update when a table
is deleted on the second pass. It seems like it checks the
first time and whatever the status is, that is what the
status remains, regardless of the actual status of the
table.

Below is the code I use to check the existence of the
table.

Function DoesObjectExist(ObjectType$, ObjectName$)
On Error Resume Next

Dim FoundObject, FindObject As String, ObjectNum As Integer
Dim DB As Database, T As TableDef
Dim Q As QueryDef, C As Container
Dim Msg As String
FoundObject = -1
Set DB = DBEngine(0)(0)

Select Case ObjectType$
Case "Tables"

FindObject = DB.TableDefs(ObjectName$).Name

Case "Queries"

FindObject = DB.QueryDefs(ObjectName$).Name

Case Else

If ObjectType$ = "Forms" Then
ObjectNum = 1
ElseIf ObjectType$ = "Modules" Then
ObjectNum = 2
ElseIf ObjectType$ = "Reports" Then
ObjectNum = 4
ElseIf ObjectType$ = "Macros" Then
ObjectNum = 5
Else
Msg = "Object Name """ & ObjectType & """ is an
invalid"
Msg = Msg & " argument to function
ObjectExists_20!"
MsgBox Msg, 16, "ObjectExists_20"
Exit Function

End If

Set C = DB.Containers(ObjectNum)
FindObject = C.Documents(ObjectName$).Name

End Select

If Err = 3265 Or FindObject = "" Then

FoundObject = 0

End If

DoesObjectExist = FoundObject

End Function
 

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

Top