我使用 MS Access 宏和查询来构建我的应用程序。我使用一些临时导入文件,需要运行宏或一些 VBA 来测试它们是否存在,如果存在,则删除它们。
我的表名称是“TempImport1”
我通过 Google 搜索对此进行了研究,并且有一些可能有效的 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
首先,您应该检查该表是否存在,然后尝试关闭它(如果存在)。然后你应该将 warnings 设置为 False,这样它就不会询问你是否确定要删除该表。
在下面的示例中,您删除Table3
. The If Not IsNull
正在检查表是否存在:
Option Compare Database
Option Explicit
Public Sub DeleteIfExists()
Dim tableName As String
tableName = "Table3"
If Not IsNull(DLookup("Name", "MSysObjects", "Name='" & tableName & "'")) Then
DoCmd.SetWarnings False
DoCmd.Close acTable, tableName, acSaveYes
DoCmd.DeleteObject acTable = acDefault, tableName
Debug.Print "Table" & tableName & "deleted..."
DoCmd.SetWarnings True
End If
End Sub
基本上代码应该可以工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)