单击按钮时删除 MS Access 表功能

问题描述

我对 VBA 编码还很陌生,因此非常感谢这里的任何帮助。

我在 MS Access 中创建了一个表单,该表单将一个 csv 文件作为一个名为“ImportTable”的新表导入,然后我运行了一堆其他查询来查看数据。

处理完数据后,我希望能够完全删除表格(作为客户端工作流程要求的一部分),但我一直在纠结如何将 delete_table 函数应用于点击事件。

我已经设法将下面的函数放在一起来删除表格,但谁能帮我指出正确的方向,如何将其应用于点击事件?

我创建的按钮名称叫做:cmdDeleteImportTable_Click()

非常感谢

Public Function table_exists(tblName As String) As Boolean

'Returns True if table exists in current DB. Else False

Dim tdf As TableDef
For Each tdf In CurrentDb.TableDefs
If StrComp(tblName,tdf.Name) = 0 Then
table_exists = True
Exit For

End If
Next tdf
End Function


Public Function delete_table(tblName As String) As Boolean
'Returns true if the table is deleted or if the table does not exist

On Error GoTo errHandler

If table_exists(tblName) Then
DoCmd.DeleteObject acTable,tblName
End If
delete_table = True
ExitSuccess:
Exit Function
errHandler:
Debug.Print Err.Number,Err.Description
Resume ExitSuccess

End Function

Sub test()
Const Names_Table = "ImportTable"
DoCmd.SetWarnings False
Debug.Print delete_table(Names_Table)
DoCmd.SetWarnings True
End Sub

解决方法

您的代码似乎正确。

您现在需要做的就是添加一个按钮。在 Access Forms 中,您必须选择按钮,然后在事件标签中,您有点击。选择它并选择代码。

这将为点击事件创建一个事件处理程序。在此事件处理程序中,您可以调用代码来导入数据或删除表。

更多细节Here