Anyway to Erase multiple forms, queries, etc in Access 2000? (In the designer that is).
Asked
Active
Viewed 1,740 times
2
-
By "erase" do you mean delete the objects or do you mean clearing the data that is displayed in an open form or query? – David-W-Fenton Jan 11 '10 at 18:56
-
Would you mind marking my answer if it helped? I could use the rep. :) – jfw Nov 21 '14 at 22:10
3 Answers
3
This worked better for me. Trying to remove the elements in the loop itself kept having trouble. I just slapped the object names into an array, then deleted them afterward.
Public Sub DeleteAllFormsAndReports()
Dim accobj As AccessObject
Dim X As Integer
Dim iObjCount As Integer
Dim sObjectNames() As String
If MsgBox("Are you sure you want to delete all of the forms and reports?", vbCritical + vbYesNo) = vbYes Then
ReDim sObjectNames(0)
For Each accobj In CurrentProject.AllForms
ReDim Preserve sObjectNames(UBound(sObjectNames) + 1)
sObjectNames(UBound(sObjectNames)) = accobj.Name
Next accobj
For X = 1 To UBound(sObjectNames)
DoCmd.DeleteObject acForm, sObjectNames(X)
Next X
ReDim sObjectNames(0)
For Each accobj In CurrentProject.AllReports
ReDim Preserve sObjectNames(UBound(sObjectNames) + 1)
sObjectNames(UBound(sObjectNames)) = accobj.Name
Next accobj
For X = 1 To UBound(sObjectNames)
DoCmd.DeleteObject acReport, sObjectNames(X)
Next X
End If
End Sub

jfw
- 140
- 6
2
I create a lot of queries on the fly when a user performs certain actions. So, I'll create the query, then delete the query once they close the form. I do this under the On Close event. It runs regardless of whether the query was created or not. So, to prevent an error, I tell it to Resume Next.
Private Sub Form_Close()
On Error Resume Next
DoCmd.Close acReport, "EmployeeDetails"
DoCmd.DeleteObject acQuery, "MyEmployeeDetails"
End Sub

Linger
- 14,942
- 23
- 52
- 79
1
You can delete objects with VBA. Be sure to step backwards when deleting from a collection, for example, this code will delete quite a few objects:
Dim db As Database
Dim idx As Long
Dim strName As String
Set db = CurrentDb
''Forms
For idx = CurrentProject.AllForms.Count - 1 To 0 Step -1
strName = CurrentProject.AllForms(idx).Name
DoCmd.DeleteObject acForm, strName
Next idx
''Reports
For idx = CurrentProject.AllReports.Count - 1 To 0 Step -1
strName = CurrentProject.AllReports(idx).Name
DoCmd.DeleteObject acReport, strName
Next idx
''Modules
For idx = CurrentProject.AllModules.Count - 1 To 0 Step -1
strName = CurrentProject.AllModules(idx).Name
If strName <> "Module9" Then
DoCmd.DeleteObject acModule, strName
End If
Next idx
''Queries
For idx = db.QueryDefs.Count - 1 To 0 Step -1
strName = db.QueryDefs(idx).Name
If Left(strName, 4) <> "~sq_" Then
db.QueryDefs.Delete strName
Else
Debug.Print strName
End If
Next idx
''Relationships
For idx = db.Relations.Count - 1 To 0 Step -1
strName = db.Relations(idx).Name
If Left(strName, 4) <> "msys" Then
db.Relations.Delete strName
Else
Debug.Print strName
End If
Next idx
''Tables
For idx = db.TableDefs.Count - 1 To 0 Step -1
strName = db.TableDefs(idx).Name
If Left(strName, 4) <> "msys" Then
db.TableDefs.Delete strName
Else
Debug.Print strName
End If
Next idx

Fionnuala
- 90,370
- 7
- 114
- 152