Access 2000 - 删除多个表单?

时间:2010-01-11 17:45:28

标签: vba ms-access access-vba ms-access-2000

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

3 个答案:

答案 0 :(得分: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

End Sub

答案 1 :(得分:2)

当用户执行某些操作时,我会动态创建大量查询。因此,我将创建查询,然后在关闭表单后删除查询。我在On Close事件下这样做。无论查询是否已创建,它都会运行。因此,为了防止出错,我告诉它继续下一步。

Private Sub Form_Close()
    On Error Resume Next
    DoCmd.Close acReport, "EmployeeDetails"
    DoCmd.DeleteObject acQuery, "MyEmployeeDetails"
End Sub

答案 2 :(得分: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