访问 VBA 代码以删除表(如果存在)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43833709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:49:32  来源:igfitidea点击:

ACCESS VBA code to delete a table if it exists

vbaaccess-vba

提问by Hilly1

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.

首先,我必须承认,我没有接受过 VBA 编码方面的培训。我使用 MS Access 宏和查询来构建我的应用程序。我使用了一些临时导入文件,需要运行宏或一些 VBA 来测试它们是否存在,如果存在,则删除它们。

My table name is "TempImport1"

我的表名是“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.

我已经通过谷歌搜索对此进行了研究,并遇到了一些可能有效的 VBA,但我在试图弄清楚如何将代码放入模块或单击子按钮时迷失了方向。我过去曾在按钮功能下剪切/粘贴 VBA 代码,并且它有效,但我不知道为什么这次它不起作用。

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.

老实说,我确定这是我对私有函数与公共函数的缺乏了解,当然还有我不了解 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

回答by Vityata

First you should check whether the table exists and then you should try to close it, if it exists. Then you should set warnings to False, so it does not ask you whether you are sure that you want to delete the table.

首先你应该检查表是否存在,然后你应该尝试关闭它,如果它存在。然后您应该将警告设置为 False,这样它就不会询问您是否确定要删除该表。

In the example below, you delete Table3. The If Not IsNullis checking whether the table exists:

在下面的示例中,您删除Table3. 在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

Pretty much the code should work.

几乎代码应该可以工作。

回答by ChrisM

To delete the TempImport1table if it exists just use the below function.

要删除TempImport1表(如果存在),只需使用以下函数。

Function DeleteTables()

    If Not IsNull(DLookup("Name", "MSysObjects", "Name='TempImport1' AND Type = 1")) Then
    DoCmd.DeleteObject acTable, "TempImport1"
    End If

End Function

Once the function has been created, create a macro, add the action run code then type in DeleteTables()in to the Function Name.
You then have a macro to run to delete the table if it exists.

创建函数后,创建一个宏,添加操作运行代码,然后输入DeleteTables()函数名称。
然后,您可以运行一个宏来删除该表(如果它存在)。