I use MS Access macros and queries to build my application. I use some temporary import files, and need to either run a macro, or some VBA, to test if they exist, and then if they do, to delete them.
My table name is "TempImport1"
I've researched this via Google searches and have some VBA that might work. I have cut/pasted VBA code under a button in the past, and it worked, but not this time. How do I put the code into a module or a click sub button?
Function IsTable(sTblName As String) As Boolean
'does table exists and work ?
'note: finding the name in the TableDefs collection is not enough,
' since the backend might be invalid or missing
On Error GoTo TrapError
Dim x
x = DCount("*", sTblName)
IsTable = True
Exit Function
TrapError:
Debug.Print Now, sTblName, Err.Number, Err.Description
IsTable = False
End Function
myTest = IsTable("table_name")
and this function tries to count the number of records on this table. If the table exists, the function will be able to make the count so will returnIsTable = True
. If not, then the error handling above will catch the error and setIsTable = False
. So the way you should use it is just to test in your code:If isTable("yourTable") Then... do something.... Else.... do something else
. – Siam