[AccessD] A2K: Loop through Forms and close open forms

Gustav Brock gustav at cactus.dk
Tue Jun 1 02:59:14 CDT 2004


Hi Darren

Another option is to loop through the opened forms only and close
these one by one (if possible):

<code>

Public Function CloseAllForms() As Boolean

' Close all open forms.
' Returns True if success.
' Reports error message for any form that could not be closed.
'
' 1999-08-02. Cactus Data ApS, CPH.

  Dim lngForms      As Long
  Dim lngForm       As Long
  Dim strForm       As String
  Dim strCaption    As String
  Dim booError      As Boolean
  Dim strMsgPrompt  As String
  Dim strMsgTitle   As String
  Dim lngMsgStyle   As Long
  
  On Error GoTo Err_CloseAllForms
  
  lngForms = Forms.Count
  For lngForm = lngForms - 1 To 0 Step -1
    ' Close forms in reverse order.
    strForm = Forms(lngForm).Name
    DoCmd.Close acForm, strForm
  Next lngForm
  
  CloseAllForms = Not booError
  
Exit_CloseAllForms:
  Exit Function
  
Err_CloseAllForms:
  strCaption = Forms(lngForm).Caption
  If Len(strCaption) > 0 Then
    strForm = strCaption
  End If
  strMsgTitle = "Error"
  strMsgPrompt = "Form '" & strForm & "' could not be closed." & vbCr & vbLf
  strMsgPrompt = strMsgPrompt & "Error:" & Str(Err.Number) & ". " & Err.Description
  lngMsgStyle = vbOKOnly + vbExclamation 'vbCritical
  DoCmd.Beep
  MsgBox strMsgPrompt, lngMsgStyle, strMsgTitle
  booError = True
  Resume Next
  
End Function

</code>

/gustav


> I want to loop through the Forms collection and close any form/forms that is/are open.

> Any suggestions?




More information about the AccessD mailing list