Need if statement in macro

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to write a macro that if a table already exists in the database it
will skip importing the table and if the table doesn't exist the macro will
import it. Any help is appreciated. Thanks.
 
Chris,

This can not be done with an If statement (and in any case, If
statements are not related to macros). In fact, it can't directly be
done in a macro at all. You would need to use VBA to write a
user-defined function in a standard module, to check the existence of
the table, and then use this function in the Condition of your macro.
Such a UDF might look something like this...

Public Function TableExists(TableName As String) As Boolean
Dim i As Integer
For i = 0 To CurrentDb.TableDefs.Count - 1
If CurrentDb.TableDefs(i).Name = TableName Then
TableExists = True
Exit For
End If
Next i
End Function

Then, in your macro Condituion, you could put the equivalent of
TableExists("YourTable")
 
Back
Top