Confirming a table exists before deleting it

G

Guest

I use a form to create a table called tblImport. If the table already exists
(from previously running the form), I use the DoCmd.DeleteObject acTable to
delete it. If the table does NOT already exist, I get an error message when
the DoCmd is executed. Is there any command that can check if a table
"exists", and then I can skip the delete if it does not.

I need to delete the table if it already exists because when the form
creates the table (it is a link to an excel file), if the table already
exists, it creates a new table and appends a number to the name (tbleImport;
tblImport1 etc.)

I tried the "Where Exists" command, but I am either doing it wrong, or it
is only for subquerries, but I can't get it to work.

I can use an on error resume next but I was hoping for something more elegent.

Thanks for any help.
Gene
 
W

Wayne Morgan

The easiest answer would probably be to just use error handling to trap the
error. You could loop through all of the tabledefs, comparing their name to
the name your looking for, but the error trapping would be quicker.
 
N

Nick Coe \(UK\)

Try wrapping your DoCmd.DeleteObject in DoCmd.SetWarnings
False and DoCmd.SetWarnings True
 
C

chris ciotti

I use a form to create a table called tblImport. If the table already
exists
(from previously running the form), I use the DoCmd.DeleteObject acTable to
delete it. If the table does NOT already exist, I get an error message when
the DoCmd is executed. Is there any command that can check if a table
"exists", and then I can skip the delete if it does not.

Hi Gene -

Access keeps a hidden table of objects (it's one of the MSys* tables) which
you can query. I can't recall the name but if you unhide the system tables
(via Tools -> Options; this might vary by Access version) and have a look you
can wrap a call to DCount in a function. If it returns 1 then the table
exists. I've done this before and it worked but I don't have the code handy
and am relying on memory (which is fragile on Sat morning :)

I don't know if this is better or worse than looping through the Tabledefs.
Good luck in any event.

--chris
 
L

Lance McGonigal

Hey...how about this:

function droptable()

on error resume next

docmd.runsql "drop table mytable;"

docmd.transfertext blah,blah

exit function

end function

cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Deleting a Table if It Exists 5
how to delete a table if it exists 2
Object Required compile error 1
VBA to Delete Table only if table exists 4
Deleting tables 9
If tabel exists 1
DLookup 8
Deleting Tables 4

Top