First, I must admit, that I am not trained in coding with VBA. 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 come across some VBA that might work, but I am lost trying to figure out how to put the code into a module or a click sub button. I have cut/pasted VBA code under a button function in the past, and it worked, but I can't figure out why it's not working this time.
Honestly, I'm sure it's my lack of understanding of private vs public functions, as well as of course, the fact that I don't know VBA.
Here's the code I'm trying to make work:
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
. – Matteo NNZ