Close all open forms

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

Whats the best way to close all open forms and open another one
can it be done within the same command button so that if someone opens
another form by clicking the relevant button the form that is already opened
should close 1st


Any Help would be great
 
This is one way

Public Function CloseAllForms(Optional ByVal ExceptName As
String = vbNullString)
Dim i As Integer
Dim x As Integer

x = Forms.Count - 1

For i = x To 0 Step -1
'The below will exclude a form if desired
If Forms(i).Name <> ExceptName Then

DoCmd.Close acForm, Forms(i).Name
End If
Next i
End Function

After this you could and an open form statement

Jim
 
Loop backwards through the Forms collection, issuing a Close on each one.

Dim lng As Long
For lng = Forms.Count - 1 To 0 Step -1
DoCmd.Close acForm, Forms(lng).Name, acSaveNo
Next

Please be aware that there is a nasty bug in Access: If an edit is in
progress, and cannot be saved (e.g. a required field has not yet been
entered), it just closes the form and you lose your entry. More info:
http://members.iinet.net.au/~allenbrowne/bug-01.html
 
Back
Top