Closes all open Form objects unless the name of the form has been passed to the function as an exclusion (you can pass multiple form names as being ‘Excluded’). Returns a byte value of 0. The reason for creating this procedure as a Function is that you have to ability to set the OnClick event of a button to: =CloseAllForms() and thus avoid a “one-liner” of [Event Procedure] VBA code to call the procedure.
Syntax
CloseAllForms([formName1][,formName2]...[,formNameX])
The optional arguments are strings that indicate form names you wish to exclude from being closed.
Public Function CloseAllForms(ParamArray Exclusions()) As Byte
'This procedure will close all the forms open except for the ones passed through
'The argument list. Only declared as a function for flexibility. No meaningful
'data is returned. Note that "Join" is used, which is not available in A97.
'
Dim strExcluded As String
Dim intX As Integer
Const cDelimiter As String = "!" 'chose ! simply because it is not allowed in
'a form name
'Concatenate all the excluded form names
strExcluded = cDelimiter & Join(Exclusions, cDelimiter) & cDelimiter
'Loop all the forms and close them as appropriate
For intX = Forms.Count - 1 To 0 Step -1
If InStr(1, strExcluded, cDelimiter & Forms(intX).Name & cDelimiter, vbTextCompare) = 0 Then
DoCmd.Close acForm, Forms(intX).Name, acSaveNo
End If
Next intX
End Function
Usage
VBA
CloseAllForms "form1", "form2"
Event Property
=CloseAllForms("form1","form2")
Remarks
The reason for creating this procedure as a Function is that you have to ability to set the OnClick event of a button to: =CloseAllForms() and thus avoid a “one-liner” of [Event Procedure] VBA code to call the procedure.
Forms not opened with DoCmd.OpenForm() will not be closed by this function.