Results 1 to 4 of 4

Thread: How find if a table in Access 2000 exists and then delete it with code?

  1. #1

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    202

    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;"

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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.

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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.

    BOFH Now, BOFH Past, Information on duplicates

    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
  •  



Click Here to Expand Forum to Full Width