【发布时间】:2017-05-07 16:12:25
【问题描述】:
首先,我必须承认,我没有接受过使用 VBA 编码的培训。我使用 MS Access 宏和查询来构建我的应用程序。我使用了一些临时导入文件,需要运行宏或一些 VBA 来测试它们是否存在,如果存在,则删除它们。
我的表名是“TempImport1”
我通过谷歌搜索对此进行了研究,并遇到了一些可能有效的 VBA,但我在试图弄清楚如何将代码放入模块或单击子按钮时迷失了方向。我过去在一个按钮功能下剪切/粘贴了 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.