Simple way to delete table if it exists?

davidb88

Board Regular
Joined
Sep 23, 2013
Messages
76
Hi -

Is there a relatively simple way to delete a table if it exists? Basically, in my database I am importing temporary files which get appended to static continuous tables each month and I would like Access to identify if there is one of these temp tables delete it, otherwise do nothing and continue with the rest of the script. Is there a simple way to do this? Right now I just am using something like this: DoCmd.DeleteObject acTable, "tblLHFItemp"
But if that temp table does not exist for whatever reason I get an error. In the end if the table doesn't exist I just want Access to skip over the error.

Thank you!
David
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I have some code that does something like that. It deletes all "error" tables that get created when importing a file (used in clean-up).
You can easily modify it to delete any table that contains any specific value in its table name (this one looks for "ImportErrors").
Code:
    Dim tblDef As TableDef


'   Delete old import errors tables
    For Each tblDef In CurrentDb.TableDefs
        If InStr(1, tblDef.Name, "ImportErrors") > 0 Then
            DoCmd.SelectObject acTable, tblDef.Name, True
            DoCmd.DeleteObject acTable, tblDef.Name
        End If
    Next tblDef
 
Upvote 0
Your welcome. Glad to help!:)
 
Upvote 0

Forum statistics

Threads
1,216,127
Messages
6,129,024
Members
449,482
Latest member
al mugheen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top