我使用MS Access的宏和查询构建我的应用程序。我使用一些临时导入文件,需要运行一个宏或一些VBA来测试它们是否存在,如果存在,就需要删除它们。
我的表名是“TempImport1”
我通过谷歌搜索研究了一些可能有效的VBA代码。过去,我曾将VBA代码剪切/复制到按钮下面,它可以工作,但这次不行。如何将代码放入模块或单击子按钮中?
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")
,此函数会尝试计算该表中记录的数量。如果该表存在,则该函数将能够进行计数,因此将返回IsTable = True
。如果不存在,则上面的错误处理将捕获错误并设置IsTable = False
。因此,您应该在代码中使用它进行测试:If isTable("yourTable") Then... do something.... Else.... do something else
。 - Matteo NNZ