[英]Ms Access - Delete all tables, queries, forms, reports and modules
I want to make a self destroyed button in my database, for security reasons.出于安全原因,我想在我的数据库中制作一个自毁按钮。 I need to modified my code so it can loop in all my tables, queries, forms, reports, module and delete them.
我需要修改我的代码,以便它可以在我所有的表、查询、表单、报告、模块中循环并删除它们。 If an error occurred, to move to the next one.
如果发生错误,则移动到下一个。
Now I am using the following code, which is an example:现在我正在使用以下代码,这是一个示例:
On error Resume Next
DoCmd.DeleteObject acTable, "tblExtra"
DoCmd.DeleteObject acTable, "tblFinances"
DoCmd.DeleteObject acTable, "tblHealth"
....
As you will see, I am using DoCmd.DeleteObject for each item I want to delete.正如您将看到的,我对要删除的每个项目使用 DoCmd.DeleteObject。
Thank you.谢谢。
This code will delete all of the objects, there might be errors along the way so you should still use on error resume next if you want.此代码将删除所有对象,在此过程中可能会出现错误,因此如果需要,您仍应在接下来的错误恢复中使用。 It closes the object then deletes:
它关闭对象然后删除:
Dim obj As AccessObject
For Each obj In CurrentProject.AllReports
Debug.Print "Deleting " & obj.Name
DoCmd.Close acReport, obj.Name, acSaveNo
DoCmd.DeleteObject acReport, obj.Name
Next
For Each obj In CurrentProject.AllForms
Debug.Print "Deleting " & obj.Name
DoCmd.Close acForm, obj.Name, acSaveNo
DoCmd.DeleteObject acForm, obj.Name
Next
For Each obj In CurrentData.AllTables
If obj.Name Not Like "MSys*" then
Debug.Print "Deleting " & obj.Name
DoCmd.Close acTable, obj.Name, acSaveNo
DoCmd.DeleteObject acTable, obj.Name
End If
Next
For Each obj In CurrentData.AllQueries
Debug.Print "Deleting " & obj.Name
DoCmd.Close acQuery, obj.Name, acSaveNo
DoCmd.DeleteObject acQuery, obj.Name
Next
For your reference, and if you want to add more objects to delete, here are the objects in CurrentData:供您参考,如果您想添加更多要删除的对象,以下是 CurrentData 中的对象:
https://msdn.microsoft.com/en-us/library/office/ff823195.aspx https://msdn.microsoft.com/en-us/library/office/ff823195.aspx
and here are the objects in CurrentProject:这是 CurrentProject 中的对象:
https://msdn.microsoft.com/en-us/library/office/ff835979.aspx https://msdn.microsoft.com/en-us/library/office/ff835979.aspx
As for Access 2016, Graham's answer randomly fails (automation error) while deleting objects.至于 Access 2016,Graham 的回答在删除对象时随机失败(自动化错误)。
It is safer to delete objects by index, from last to first.按索引从最后到第一个删除对象更安全。 The checkonly parameter is just for testing purposes, in case you erase any important object.
checkonly 参数仅用于测试目的,以防您删除任何重要对象。
I've tested both solutions in Access 2016 MSO (16.0.8431.2110) 32 bits.我已经在 Access 2016 MSO (16.0.8431.2110) 32 位中测试了这两种解决方案。
Public Sub deleteObjects(Optional ByVal CheckOnly As Boolean = True)
Dim obj As AccessObject
Dim i As Long
With CurrentProject
For i = .AllReports.count - 1 To 0 Step -1
Set obj = .AllReports(i)
Debug.Print "Deleting " & obj.Name
If Not CheckOnly Then
DoCmd.Close ObjectType:=acReport, ObjectName:=obj.Name, Save:=acSaveNo
DoCmd.DeleteObject acReport, obj.Name
End If
Next
For i = .AllForms.count - 1 To 0 Step -1
Set obj = .AllForms(i)
If obj.Name <> "myImportantForm" Then
Debug.Print "Deleting " & obj.Name
If Not CheckOnly Then
DoCmd.Close ObjectType:=acForm, ObjectName:=obj.Name, Save:=acSaveNo
DoCmd.DeleteObject acForm, obj.Name
End If
End If
Next
For i = .AllModules.count - 1 To 0 Step -1
Set obj = .AllModules(i)
If obj.Name <> "myImportantModule" Then
Debug.Print "Deleting " & obj.Name
If Not CheckOnly Then
DoCmd.DeleteObject acModule, obj.Name
End If
End If
Next
End With
With CurrentData
For i = .AllQueries.count - 1 To 0 Step -1
Set obj = .AllQueries(i)
Debug.Print "Deleting " & obj.Name
If Not CheckOnly Then
DoCmd.Close ObjectType:=acQuery, ObjectName:=obj.Name, Save:=acSaveNo
DoCmd.DeleteObject acQuery, obj.Name
End If
Next
For i = .AllTables.count - 1 To 0 Step -1
Set obj = .AllTables(i)
If Not obj.Name Like "MSys*" Then
Debug.Print "Deleting " & obj.Name
If Not CheckOnly Then
DoCmd.Close ObjectType:=acTable, ObjectName:=obj.Name, Save:=acSaveNo
DoCmd.DeleteObject acTable, obj.Name
End If
End If
Next
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.