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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop MS Access table if it exist

Status
Not open for further replies.

tdrBates

MIS
Nov 22, 2002
60
US
Can I run code similar to the example below?

If tablename exist then
Run.SQL "DROP TABLE tablename"
End if
 
Assuming that the table exists then the following deletes it.
You should check for table first and, depending on what you are
doing you may want to ask user whether they want to delete it.

db.Execute "DROP TABLE [Archive2];"
 
One, admittedly inelegant, way of checking a table exists is to make use of the hidden system file MSysObjects, thus:

Code:
If DCount("*", "MSysObjects","Name='Archive2' AND Type=1") = 1 Then
    DBEngine(0)(0).Execute "DROP TABLE [Archive2];"
End If

Another way that doesn't depend on the tiresome DCount is just to turn error-handling off before the DROP TABLE so that it doesn't fall over when the non-existant table can't be deleted. Don't forget to turn it back on again afterwards... [smile] [pc2]
 
Agreed. Or, you can use the tabledef approach:

For Each tdf In db.TableDefs()
If tdf.Name = "Archive2" Then
'drop table here
Next tdf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top