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.
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:
https://msdn.microsoft.com/en-us/library/office/ff823195.aspx
and here are the objects in CurrentProject:
https://msdn.microsoft.com/en-us/library/office/ff835979.aspx
As for Access 2016, Graham's answer randomly fails (automation error) while deleting objects.
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.
I've tested both solutions in Access 2016 MSO (16.0.8431.2110) 32 bits.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.