Link to home
Start Free TrialLog in
Avatar of marlind
marlind

asked on

What is the command to check for a table and then give the user a message if it is not.

I would like my database to check for table crexport and if it is not there I need it to display instructions (msgbox) for the user to follow. If it is there I will have it "do something". Thanks for any help.
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

you can use a function like this

Function TableExists(TableName as string) as boolean
dim db as database, tbl as tabledef
set db as currentdb()

tableexists=false

          For Each tbl In db.TableDefs
               If tbl.Name = TableName Then
                    tableExists = True
                    Exit Function
               End If
          Next tbl

End Function
You need a function for this - this will do it:

Function ExistsTable(strTableName As String) As String
ExistsTable=Not IsNull(DLookup("[Name]","MSysObjects","[Name]='" & strTableName & "' AND Type=6"))
End Function

You can now do:

If ExistsTable("MyTable")=False Then
   MsgBox "Do something!"
End If
This line:
Function ExistsTable(strTableName As String) As String

should obviously read:
Function ExistsTable(strTableName As String) As Boolean

:-)
Avatar of marlind
marlind

ASKER

shanesuebsahakarn,
I have this
Function ExistsTable(strTableName As String) As Boolean
ExistsTable = Not IsNull(DLookup("[Name]", "MSysObjects", "[Name]='" & strTableName & "' AND Type=6"))
End Function
I have this on a command button
If ExistsTable("CRExport") = False Then
   MsgBox "Do something!", vbCritical, "Quality Control"
The message box appears even if the table is there. I would like a frmuserselection to open if the table is there, and the msgbox to appear if the table is not there.  I played around with the if statement put couldn't get my form to come up. I must be missing something.
On Shane's answer,

I think type for tables is = 1, then:

Function ExistsTable(strTableName As String) As Boolean
ExistsTable=Not IsNull(DLookup("[Name]","MSysObjects","[Name]='" & strTableName & "' AND Type=1"))
End Function
ASKER CERTIFIED SOLUTION
Avatar of shanesuebsahakarn
shanesuebsahakarn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nestorio beat me to it :)
Avatar of marlind

ASKER

shanesuebsahakarn,
Perfect. Now I have to figure out what my do something instructions should be. Thanks for the help.
marlind
did you try my post?

If TableExists("CRExport")=False Then
  MsgBox "Do something"
else
  Docmd.OpenForm "frmuserselection"
end if
Avatar of marlind

ASKER

It may work too but I chose shanesuebsahakarn answer first. Thank you for giving the replying to my question.
marlind
It will work definitely, and sad to say that i posted first and never got any chance.