2

无论如何要擦除 Access 2000 中的多个表单、查询等?(在设计师中)。

4

3 回答 3

3

这对我来说效果更好。试图删除循环本身中的元素一直遇到麻烦。我只是将对象名称拍成一个数组,然后将它们删除。

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

结束子

于 2013-10-08T19:07:29.950 回答
2

当用户执行某些操作时,我会即时创建很多查询。因此,我将创建查询,然后在他们关闭表单后删除查询。我在 On Close 事件下执行此操作。无论是否创建了查询,它都会运行。所以,为了防止出错,我告诉它继续下一步。

Private Sub Form_Close()
    On Error Resume Next
    DoCmd.Close acReport, "EmployeeDetails"
    DoCmd.DeleteObject acQuery, "MyEmployeeDetails"
End Sub
于 2012-03-12T18:02:39.610 回答
1

您可以使用 VBA 删除对象。从集合中删除时一定要后退一步,例如,此代码将删除相当多的对象:

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
于 2010-01-11T22:43:02.123 回答