checking for existence of access table using SQL in VBA

presence76

Board Regular
Joined
May 11, 2004
Messages
76
I have a problem. I have an ACCESS DB that I run daily. One of the things it does is creates a table that is used temporarily. Then, on the next run, I delete it before I begin processing. I have a new situation where this table might not exist (such as the first day of a new install) and I do not want the DB to abend in the middle of a process just because this table does not exist.

I have tried several different techniques.

Here is the basic command I use to delete the table:

DoCmd.RunSQL "DROP TABLE tbltracerfinal;"

I then changed it to :

DoCmd.RunSQL "DROP TABLE tbltracerfinal;"
On Error Resume Next

This does not work as VB kicks an SQL error statement before it interprets the next line (ON Error....)

I tried several different versions of SQL code that I thought would work like:

'DoCmd.RunSQL "If (tblTracerfinal) then DROP TABLE tblTracerfinal;"

This has faulty SQL code.

Does anyone know how to check for the existence of a table using SQL in a VBA environment?
OR
Does anyone know how I could do it using VBA code???

Any help would be greatly appreciated. Thanks.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I found it.

On Error Resume Next needs to be placed on the line BEFORE the line it is being applied to.
 
Upvote 0
The resume next needs to come before the error! Try
On Error Resume Next
DoCmd.DeleteObject acTable, "tblTracerfinal"

HTH

Peter
 
Upvote 0
Try the following function:
Code:
Function IsTable(tblName As String) As Boolean
' returns true if there is a table called tblName

Dim db As Database
Dim tdf As TableDef

    Set db = CurrentDb

    For Each tdf In db.TableDefs
        IsTable = (tdf.Name = tblName)
        If IsTable Then Exit Function
    Next
    
    Set tdf = Nothing
    Set db = Nothing
    
End Function
 
Upvote 0

Forum statistics

Threads
1,221,805
Messages
6,162,074
Members
451,738
Latest member
gaseremad

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