How to avoid an error deleting a table that doesn't exist?

Balr14

Registered User.
Local time
Today, 17:54
Joined
Oct 30, 2008
Messages
34
I have a macro that imports some files and massages data. It works fine except for a couple things. The possibility exists that the data being imported will contain some crap that will cause an import error table to be created. I don't care about the table, so I have a "delete object" action for this import error table at the beginning of my macro. The problem is that if the table doesn't get created, the macro fails the next time it's run because it can't find the named object.

Is there another way to delete a table in a macro that won't cause an error if the table isn't there?
 
Around the code that attempts to delete the object, you might be able to implement the following code around it.

On Error Resume Next
(your line of code to delete the object)
On Error Goto 0

I have used this code successfully in similar situations. The 'On Error Resume Next' tells Excel to continue execution of the code upon an error. The 'On Error Goto 0' clears down the error handler....at least thats the theory!
 
I use the following method.

In modules create a new module (I usually call it functions) and type the following code

Code:
Public Function find_files(tablename As String) As String

Dim dbcurr As Database, intfiles As Integer

find_files = 0

Set dbcurr = CurrentDb

For intfiles = 0 To dbcurr.TableDefs.Count - 1

    If dbcurr.TableDefs(intfiles).Name = tablename Then
        find_files = 1
    End If
    
Next intfiles


End Function
In the condition of the deleteobject line (you may have to turn this view on by selecting View - Conditions) type

find_files("yourfilename")=1

If the table exists then a value of 1 is returned and deleteobject is executed, if the table does not exist then a value of 0 is returned and deleteobject is not executed.
 
Thanks a lot, guys. I see how to make that work, now.
 

Users who are viewing this thread

Back
Top Bottom