Access/VBA Library

CloseAll

Closes alle open forms, tables, queries etc.

Syntax

CloseAll( [doNotCloseForm] )
Parameters
Name Type Description
doNotCloseForm String Optional. Name of the form not be to closed by this procedure.
Default: ""
Code
Public Sub CloseAll(Optional ByVal doNotCloseForm As String = "")
    
    Dim aob As AccessObject

On Error GoTo CloseAll_Error


    With CurrentData
       ' "Tables"
       For Each aob In .AllTables
           If aob.IsLoaded Then
               DoCmd.Close acTable, aob.name, acSaveYes
           End If
       Next aob
     
       ' "Queries"
       For Each aob In .AllQueries
           If aob.IsLoaded Then
               DoCmd.Close acQuery, aob.name, acSaveYes
           End If
       Next aob
    End With
     
     
    With CurrentProject
       ' "Forms"
       For Each aob In .AllForms
           If aob.IsLoaded Then
                If aob.name <> doNotCloseForm Then
                    ' Do not close hidden forms
                    If Forms(aob.name).visible Then
                        DoCmd.Close acForm, aob.name, acSaveYes
                    End If
                End If
           End If
       Next aob
     
       ' "Reports"
       For Each aob In .AllReports
           If aob.IsLoaded Then
               DoCmd.Close acReport, aob.name, acSaveYes
           End If
       Next aob
     
       ' "Pages"
       For Each aob In .AllDataAccessPages
           If aob.IsLoaded Then
               DoCmd.Close acDataAccessPage, aob.name, acSaveYes
           End If
       Next aob
     
       ' "Macros"
       For Each aob In .AllMacros
           If aob.IsLoaded Then
               DoCmd.Close acMacro, aob.name, acSaveYes
           End If
       Next aob
    End With

CloseAll_Exit:
    On Error Resume Next
    
    Exit Sub

CloseAll_Error:
    Call ShowError(functionName:="CloseAll")
    Resume CloseAll_Exit

End Sub

Remarks

  • Hidden forms are not closed.
Example
' Close everything on open of a form
Private Sub Form_Open(Cancel As Integer)
   Call CloseAll(Me.name)
   Call MoveTo_TopLeft(Me)
End Sub