How find if a table in Access 2000 exists and then delete it with code?
As the title states, how can I check with Jet SQL or somehow with VBA code in Access 2000 if a local Access table exists as context object with a given name?
And if exists, then I delete it with running query like "DROP TABLE;"
Re: How find if a table in Access 2000 exists and then delete it with code?
If you run an SQL statement like this:
Code:
SELECT * FROM tableName WHERE 1 = 2
...it will not return any records (because the Where clause blocks everything), but it also wont give an error if the table exists.
You should get an error if the table doesn't exist, so you just need to handle that error appropriately.
Re: How find if a table in Access 2000 exists and then delete it with code?
Quote:
You should get an error if the table doesn't exist, so you just need to handle that error appropriately.
in that case you could just delete the table and handle any error without testing for existance
you should be able to use an ADOX catalog to return all the table names within a database
Code:
Set xcat = CreateObject("adox.catalog")
Set xcat.ActiveConnection = cn
For i = 0 To xcat.tables.Count - 1
'check if the table is a user defined table
If xcat.tables.Item(i).Type = "TABLE" Then
Debug.Print xcat.tables(i).Name
End If
Next i
Re: How find if a table in Access 2000 exists and then delete it with code?
Inside Access - DAO : TableDefs
Code:
On Error Resume Next
CurrentDB.TableDefs("namehere").delete
On Erro Goto 0
Something like that (untested and from what I remember)
Not sure you'd want to delete it though, no way to retrieve if you are wrong...
Also TableDefs should work for linked table too, without deleting data, just the link to it.