[AccessD] Closing Forms

Gustav Brock gustav at cactus.dk
Sun Mar 28 03:37:16 CST 2004


Hi Martin

> At times I have 3 or 4 forms open at once and may want to return to the main menu form from any of those and at the same time closing all forms except the main menu form. Can anyone tell me why the
> following function after closing only one form via the Case else will not continue to work through the forms group and close multiple forms?

> Public Function CloseForms(FromForm As String)
>     Dim frm As Form
>     For Each frm In Forms
>         Select Case frm.Name
>             Case Is = "frmMainMenu"
>                 'Dont close it
>             Case Is = FromForm
>                 'Dont close the form that called this yet
>             Case Else
>                 'Close any other form
>                 MsgBox ("The form being closed is " & frm.Name)
>                 DoCmd.close acForm, frm.Name, acSaveNo
>         End Select
>     Next frm
> End Function

You are probably destroying the collection Forms moving forwards.
Most solutions to this task browse backwards through the collection
like this:

<code>

Public Function CloseForms(FromForm As String)

    Dim strForm  As String
    Dim lngForm  As Long
    
    For lngForm = Forms.Count - 1 To 0 Step -1
        strForm = Forms(lngForm).Name
        Select Case strForm
            Case Is = "frmMainMenu"
                'Dont close it
            Case Is = FromForm
                'Dont close the form that called this yet
            Case Else
                'Close any other form
                MsgBox ("The form being closed is " & strForm)
                DoCmd.close acForm, strForm, acSaveNo
        End Select
    Next

End Function

</code>

/gustav




More information about the AccessD mailing list