[AccessD] Close All Forms

Gustav Brock gustav at cactus.dk
Sat Aug 20 16:53:28 CDT 2005


Hi Barbara

First, there's no need to retrieve the Form collection count that many
times.
Second, your main form is probably the first form to open, so why not
just leave it?

Thus:

    Function CloseAllForms()
      Dim lngForms As Long
      Dim lngForm  As Long
      lngForms = Forms.Count
      ' Loop through the collection of forms and close 
      ' them except for the first opened (main form).
      For lngForm = lngForms - 1 To 1 Step -1
        DoCmd.Close acForm, Forms(lngForm).Name
      Next
    End Function

You cannot do this forward because if you close form N all form indexes
above N will be shifted one down to fill the hole in numbers. This
could, however, be taken advantage of:

    Function CloseAllForms()
      ' Close all forms except the first opened.
      While Forms.Count > 1
        DoCmd.Close acForm, Forms(1).Name
      Wend
    End Function

As you can see, your function seems to be a mix of these two approaches.

/gustav

>>> BarbaraRyan at cox.net 19-08-05 20:51 >>>
I have used the following code successfully in Access 97 to close all
forms when a specific function key is pressed.  However, in Access 2002,
this code causes the error box to appear (with the 2 options to send an
error report to Microsoft or not) and then closes Access.  Does anyone
know why?

    Function CloseAllForms()
        Dim i As Integer
        ' Loop through the collection of open forms and close them
(except for the main menu)
        If Forms.Count > 0 Then
            For i = Forms.Count - 1 To 0 Step -1
                If Forms(i).Name <> "frmMainMenu" Then
                    DoCmd.Close acForm, Forms(i).Name
                End If
            Next i
        End If
    End Function

Thanks,
Barb Ryan




More information about the AccessD mailing list