I would like to Close All Open Objects (Forms, Reports, Tables, Queries...) with a custom code. A quick internet search found an old thread from 2007 on this forum (http://bytes.com/topic/access/answer...rms-tables-etc) in which "@ADezii" provided a great solution which I have placed in a Public Function:
I've then successfully called the code with a single line on an Event Procedure with the line:
which then successfully closed all open objects.
I'd like to pass an exception to the function to allow a particular form to remain opened but I'm puzzled about how to accomplish this. I was thinking about changing line 4 above to:
in order to except the exception, but then I don't understand what else to modify in the function to test the exception and skip the closing procedure.
The purpose I'll be using this for is to log out of everything and return to the "frmLogOn" though I may find other uses for this later in life.
Thank you in advance for volunteering your expertise to others!
Code:
Option Compare Database
Option Explicit
Public Function CloseAllObjects()
On Error GoTo errHandler
'------------------------------------------------------------
'Purpose: Closes all Access Objects (Forms, Reports, Tables Queries)
'Author: User: ADezii
'Date: 03/10/2007
'Source: http://bytes.com/topic/access/answers/614454-possible-use-vba-code-close-all-open-objects-forms-tables-etc
'------------------------------------------------------------
Dim aob As AccessObject
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
DoCmd.Close acForm, aob.Name, acSaveYes
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
' "Modules"
For Each aob In .AllModules
If aob.IsLoaded Then
DoCmd.Close acModule, aob.Name, acSaveYes
End If
Next aob
End With
errExit:
Exit Function
errHandler:
MsgBox "Error " & Err.Number & " " & Err.Description
Resume errExit
End Function
Code:
CloseAllObjects
I'd like to pass an exception to the function to allow a particular form to remain opened but I'm puzzled about how to accomplish this. I was thinking about changing line 4 above to:
Code:
Public Function CloseAllObjects(Optional strException As String)
The purpose I'll be using this for is to log out of everything and return to the "frmLogOn" though I may find other uses for this later in life.
Thank you in advance for volunteering your expertise to others!
Comment