删除MS Access中除一个以外的所有表(2007-2013)

问题描述 投票:0回答:1

我正在尝试删除除Access中的一个表之外的所有表。这是在本地完成的,因此没有链接表,也没有关系。我要保留的表是“Log”(历史记录)。

我打算删除三个表和两个导入错误表。这是一个月度过程,导入错误表可能每月命名不同,因此一些菊花链式DROP TABLE“Table1_ImportErrors”查询不一定有效。

代码here似乎不适用,因为我只能真正命名我要保留的表(这表示按名称删除表 - 在导入错误的情况下我不会这样)。

我现在拥有的是:

-dbo.Table1

-dbo.Table2

-dbo.Table3

-dbo.T2Sheetname_ImportErrors

-dbo.T3Sheetname_ImportErrors

-dbo.Log

-9查询

-4宏

-2个模块

我想保留所有查询,宏,模块和dbo.Log。

我刚刚学习VBA,但不知道该怎么做。打开以使用此处的查询或其他方法。我感谢任何意见,很乐意回答任何问题。

编辑:以下为我工作。

Private Sub
DoCmd.DeleteObject acTable, "Table1"
DoCmd.DeleteObject acTable, "Table2"
DoCmd.DeleteObject acTable, "Table3"

Dim tdf as TableDef
    For Each tdf In CurrentDb.TableDefs
        If Right(tdf.Name, 12) = "ImportErrors" Then
            DoCmd.DeleteObject acTable, tdf.Name
        End If
    Next tdf
End Sub
ms-access access-vba
1个回答
1
投票

以下是谷歌搜索一些短语的结果,如“ms access for all tables”和“ms access vba delete table”。使用谷歌,这是你的朋友。

根据msgbox sez的不同,您可能需要过滤“dbo.Log”或“Log”。

'From  https://stackoverflow.com/questions/17555174/how-to-loop-through-all-tables-in-an-ms-access-db 

    Dim db As DAO.Database
    Dim tdf As DAO.TableDef
    Set db = CurrentDb
    For Each tdf In db.TableDefs
        ' ignore system and temporary tables
        If Not (tdf.name Like "MSys*" Or tdf.name Like "~*") Then
            If tdf.name <> "Log" Then
            MsgBox("deleting " & tdf.name)
            '  from https://stackoverflow.com/questions/15945297/what-is-the-difference-between-docmd-deleteobject-actable-vs-drop-table
            'DoCmd.DeleteObject acTable, tdf.name
        End If
        End If
    Next
    Set tdf = Nothing
    Set db = Nothing
© www.soinside.com 2019 - 2024. All rights reserved.