How to close objects

  • Thread starter Thread starter Maria Joao
  • Start date Start date
M

Maria Joao

I want my database to close all open forms, before
opening one form.

How can I do this, since several forms can be opened.

Thanks

Maria Joao
 
Loop backwards through the Forms collection until none are left open.

This example closes everything except the form that contains this code:

Private Sub cmdQuit_Click()
On Error GoTo Err_Handler
'Purpose: Close all open forms and quit.
Dim frm As Form
Dim lngKt As Long
Dim lngI As Long

lngKt = Forms.Count - 1&
If lngKt >= 0& Then
For lngI = lngKt To 0& Step -1&
Set frm = Forms(lngI)
If frm.Name <> Me.Name Then
Set frm = Forms(lngI)
If HasProperty(frm, "Dirty") Then
If frm.Dirty Then
frm.Dirty = False
End If
End If
Set frm = Nothing
DoCmd.Close acForm, Forms(lngI).Name
End If
Next
End If
lngI = -1&
'DoCmd.Quit acQuitSaveNone

Exit_Handler:
Exit Sub

Err_Handler:
If lngI > -1& Then
MsgBox "Unable to close the form: " & Forms(lngI).Caption & vbCrLf &
vbCrLf & _
"The error message is:" & vbCrLf & Err.Description,
vbExclamation, "Cannot close"
Else
Call LogError(Err.Number, Err.Description, conMod & "cmdQuit_Click")
End If
Resume Exit_Handler
End Sub
Public Function HasProperty(obj As Object, strPropName As String) As Boolean
'Purpose: Return true if the object has the property.
Dim vardummy As Variant

On Error Resume Next
vardummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function
 
Back
Top