如何一次删除所有ms访问表中的数据?

时间:2009-03-26 09:46:11

标签: sql ms-access

MS-Access中是否有一种方法可以一次删除所有表中的数据。我们在访问中运行数据库,每月保存数据,然后删除访问中的所有数据。但它需要从许多表中删除数据。是不是有更简单/更简单的方法呢?

6 个答案:

答案 0 :(得分:16)

为什么不随身携带数据库的空副本。在月末,保存现有数据库,然后将空数据库复制到其位置。

答案 1 :(得分:11)

克雷格的回答简单而明智。如果您确实需要编程解决方案,则以下VBA脚本将清除除隐藏表之外的每个表中的所有数据。它需要启用DAO - 在Visual Basic编辑器中,转到工具 - >引用,并勾选Microsoft DAO 3.6 Object Library,然后确定:

Public Sub TruncateTables()
'Majority of code taken from a data dictionary script I can no longer source nor find the author

On Error GoTo Error_TruncateTables

Dim DB As DAO.Database
Dim TDF As DAO.TableDef
Dim strSQL_DELETE As String

Set DB = CurrentDb()

    For Each TDF In DB.TableDefs
        If Left(TDF.Name, 4) <> "MSys" Then
            strSQL_DELETE = "DELETE FROM " & TDF.Name & ";"
            DB.Execute strSQL_DELETE
        End If
    Next

MsgBox "Tables have been truncated", vbInformation, "TABLES TRUNCATED"
DB.Close

Exit_Error_TruncateTables:
    Set TDF = Nothing
    Set DB = Nothing
    Exit Sub

Error_TruncateTables:
    Select Case Err.Number
        Case 3376
            Resume Next 'Ignore error if table not found
         Case 3270 'Property Not Found
            Resume Next
        Case Else
            MsgBox Err.Number & ": " & Err.Description
            Resume Exit_Error_TruncateTables
    End Select
End Sub

答案 2 :(得分:9)

Alistair的答案很好,虽然需要更新。旧的if语句会导致错误,旧的动态字符串不能用于名称有空格的表。它会将“人物信息”这样的名称视为“人”。我已经更新了代码,并且如果你想让一些表保留他们的数据,那么在if语句中添加异常会更容易一些。

 Public Sub TruncateTables()
    'Majority of code taken from a data dictionary script I can no longer source nor find the author

    On Error GoTo Error_TruncateTables

    Dim DB As DAO.Database
    Dim TDF As DAO.TableDef
    Dim strSQL_DELETE As String

    Set DB = CurrentDb()

        For Each TDF In DB.TableDefs
            If Not (TDF.Name Like "MSys*" Or TDF.Name Like "~*" Or Len(TDF.Connect) > 0) Then
                'This will prevent system, temporary and linked tables from being cleared
                strSQL_DELETE = "DELETE FROM " & "[" & TDF.Name & "]"
                DB.Execute strSQL_DELETE
            End If
        Next

    MsgBox "Tables have been truncated", vbInformation, "TABLES TRUNCATED"
    DB.Close

    Exit_Error_TruncateTables:
        Set TDF = Nothing
        Set DB = Nothing
        Exit Sub

    Error_TruncateTables:
        Select Case Err.Number
            Case 3376
                Resume Next 'Ignore error if table not found
             Case 3270 'Property Not Found
                Resume Next
            Case Else
                MsgBox Err.Number & ": " & Err.Description
                Resume Exit_Error_TruncateTables
        End Select
    End Sub

答案 3 :(得分:0)

由于这是一个重复的操作,如果你制作一个简单的SQL脚本来做这件事会更好。

DELETE FROM <table1>;
DELETE FROM <table2>;
...
DELETE FROM <tablen>;

答案 4 :(得分:0)

突出显示所有ROWS,然后按键盘上的Delete键。如果访问正在执行该操作,则不会让您转到底部,然后进入单元格并单击ctrl +向下箭头。要突出显示所有行,请突出显示顶行,然后滚动到底行,并在选择底行时按住shift。应突出显示所有行。

答案 5 :(得分:0)

这将删除除系统表

之外的所有表中的所有数据
Dim T As TableDef
DoCmd.SetWarnings False
For Each T In CurrentDb.TableDefs
    If T.Name Like "d2s_*" Then
        DoCmd.RunSQL "DELETE * FROM " & T.Name
    End If
Next T
DoCmd.SetWarnings True

另一种方法:(基于对Christopher Duke的建议)

Dim T As TableDef
DoCmd.SetWarnings False
For Each T In CurrentDb.TableDefs
    If Not Left(T.Name, 4) = "MSys" Then
        DoCmd.RunSQL "DELETE * FROM [" & T.Name & "]"
    End If
Next T
DoCmd.SetWarnings True