Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to test if a certain table exists?

Status
Not open for further replies.

bakfram

Technical User
Sep 20, 2002
2
FI
I want to start a macro by deleting a table. How can I test
for the tables existence before deleting it?

Thanks
 
Hi

Well you could just use "DROP TABLE" SQL

Dim DB as Database
Db.Execute "DROP TABLE MyTable;"

this will not report an error if MyTable does not exist,

Or you could use
On Error GoTo XXX
If db.Tabledefs("MyTable") Then
docmd.runsql "DROP TABLE MyTAble;"
End If
XXX:

but I think the first way is neater

Regards
Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
UK
kenneth.reaySPAMNOT@talk21.com
remove SPAMNOT to use
 
To solve this particular problem, you can use this function:

Function TableDeleted(TableNameAs String) As Boolean
On Error ResumeNext
DoCmd.DeleteObject acTable, TableName
End Function
Paste it into a module and save it

In your macro:
Action = RunCode
FunctionName: TableDeleted("YourTableName")

You don't need the Delete action anymore. The function does the job.
Note: The table must be closed and not used by any other object when you run the macro. But you're using macros, so I suppose it's a single user application, and it's Saturday afternoon-relaxing time for me which I wish you too...


And second step: drop macros and get VBA coding training...
[pipe]
Daniel Vlas
Systems Consultant
danvlas@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top