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.
LVL 2
marlindAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rey Obrero (Capricorn1)Commented:
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
0
shanesuebsahakarnCommented:
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
0
shanesuebsahakarnCommented:
This line:
Function ExistsTable(strTableName As String) As String

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

:-)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

marlindAuthor Commented:
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.
0
NestorioCommented:
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
0
shanesuebsahakarnCommented:
My apologies, the code should be:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shanesuebsahakarnCommented:
Nestorio beat me to it :)
0
marlindAuthor Commented:
shanesuebsahakarn,
Perfect. Now I have to figure out what my do something instructions should be. Thanks for the help.
0
Rey Obrero (Capricorn1)Commented:
marlind
did you try my post?

If TableExists("CRExport")=False Then
  MsgBox "Do something"
else
  Docmd.OpenForm "frmuserselection"
end if
0
marlindAuthor Commented:
It may work too but I chose shanesuebsahakarn answer first. Thank you for giving the replying to my question.
0
Rey Obrero (Capricorn1)Commented:
marlind
It will work definitely, and sad to say that i posted first and never got any chance.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.