-
Jul 17th, 2018, 08:24 AM
#1
Thread Starter
Addicted Member
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;"
-
Jul 17th, 2018, 11:16 AM
#2
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.
-
Jul 18th, 2018, 07:28 AM
#3
Re: How find if a table in Access 2000 exists and then delete it with code?
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
Last edited by westconn1; Jul 18th, 2018 at 07:33 AM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 18th, 2018, 08:20 AM
#4
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.
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|