Close all open forms except 1

G

Guest

I have a lot of different forms that can be opened from a form called "Client
screen" which all contain data relative to that client. I would like the
main "Client screens" form close button, to close all forms except form "Main
Menu". It wouldn't be a problem to close the form "Main Menu" and reopen it
but I don't know how to refer to all forms to close them.
I have tried the response to the question "Check and close all forms except
the Switchboard" but the code didn't work. I do not have a Switchboard, just
a standard form called "Main Menu". Is this the problem?
I am using Access 2003 and would be most appreciative of any help
 
D

Douglas J. Steele

Dim lngLoop As Long

For lngLoop = (Forms.Count - 1) To 0 Step -1
If Forms(lngLoop).Name <> "Main Menu" Then
DoCmd.Close acForm, Forms(lngLoop).Name, acSaveNo
End If
Next lngLoop
 
A

Allen Browne

This code will close all forms except the main menu:
Dim i As Integer
For i = Forms.Count - 1 to 0
If Forms(i).Name <> "Main Menu" Then
DoCmd.Close acForm, Forms(i).Name
End If
Next

The Forms collection contains all open forms. The code loops backwards
through this collection, because the loop gets confused if you close the
lower count first.

Be aware that if any form is dirty with an entry that cannot be saved (e.g.
a required field is missing), Access just loses the entry with no warning.
More info in:
Losing data when you close a form
at:
http://allenbrowne.com/bug-01.html

You could also attack this issue from the other direction: let the user have
as many forms and reports open as they like but once they close the last
one, you automatically open your Main Menu again:

Public Function Keep1Open(objMe As Object)
On Error GoTo Err_Keep1Open
'Purpose: Open the Switchboard if nothing else is visible.
'Argument: The object being closed.
'Return: None.
'Usage: In the OnClose property of forms and reports:
' =Keep1Open([Form])
' =Keep1Open([Report])
Dim sName As String 'Name of the object being closed.
Dim lngObjType As Long 'acForm or acReport
Dim bFound As Boolean 'Flag not to open the switchboard.
Dim frm As Form 'an open form.
Dim rpt As Report 'an open report.

'Initialize
sName = objMe.Name
If TypeOf objMe Is Form Then
lngObjType = acForm
ElseIf TypeOf objMe Is Report Then
lngObjType = acReport
End If

'Any other visible forms?
For Each frm In Forms
If frm.Visible Then
If frm.Name <> sName Or lngObjType <> acForm Then
bFound = True
Exit For
End If
End If
Next

'Any other visible reports?
If Not bFound Then
For Each rpt In Reports
If rpt.Visible Then
If rpt.Name <> sName Or lngObjType <> acReport Then
bFound = True
Exit For
End If
End If
Next
End If

'If none found, open the switchboard.
If Not bFound Then
DoCmd.OpenForm "Main Menu"
End If

Exit_Keep1Open:
Set frm = Nothing
Set rpt = Nothing
Exit Function

Err_Keep1Open:
If Err.Number <> 2046& Then 'OpenForm is not available when closing
database.
MsgBox "Error " & Err.Number & ": " &Err.Description
End If
Resume Exit_Keep1Open
End Function
 
G

Guest

Thank you both very much.

Allen Browne said:
This code will close all forms except the main menu:
Dim i As Integer
For i = Forms.Count - 1 to 0
If Forms(i).Name <> "Main Menu" Then
DoCmd.Close acForm, Forms(i).Name
End If
Next

The Forms collection contains all open forms. The code loops backwards
through this collection, because the loop gets confused if you close the
lower count first.

Be aware that if any form is dirty with an entry that cannot be saved (e.g.
a required field is missing), Access just loses the entry with no warning.
More info in:
Losing data when you close a form
at:
http://allenbrowne.com/bug-01.html

You could also attack this issue from the other direction: let the user have
as many forms and reports open as they like but once they close the last
one, you automatically open your Main Menu again:

Public Function Keep1Open(objMe As Object)
On Error GoTo Err_Keep1Open
'Purpose: Open the Switchboard if nothing else is visible.
'Argument: The object being closed.
'Return: None.
'Usage: In the OnClose property of forms and reports:
' =Keep1Open([Form])
' =Keep1Open([Report])
Dim sName As String 'Name of the object being closed.
Dim lngObjType As Long 'acForm or acReport
Dim bFound As Boolean 'Flag not to open the switchboard.
Dim frm As Form 'an open form.
Dim rpt As Report 'an open report.

'Initialize
sName = objMe.Name
If TypeOf objMe Is Form Then
lngObjType = acForm
ElseIf TypeOf objMe Is Report Then
lngObjType = acReport
End If

'Any other visible forms?
For Each frm In Forms
If frm.Visible Then
If frm.Name <> sName Or lngObjType <> acForm Then
bFound = True
Exit For
End If
End If
Next

'Any other visible reports?
If Not bFound Then
For Each rpt In Reports
If rpt.Visible Then
If rpt.Name <> sName Or lngObjType <> acReport Then
bFound = True
Exit For
End If
End If
Next
End If

'If none found, open the switchboard.
If Not bFound Then
DoCmd.OpenForm "Main Menu"
End If

Exit_Keep1Open:
Set frm = Nothing
Set rpt = Nothing
Exit Function

Err_Keep1Open:
If Err.Number <> 2046& Then 'OpenForm is not available when closing
database.
MsgBox "Error " & Err.Number & ": " &Err.Description
End If
Resume Exit_Keep1Open
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Simon said:
I have a lot of different forms that can be opened from a form called
"Client
screen" which all contain data relative to that client. I would like the
main "Client screens" form close button, to close all forms except form
"Main
Menu". It wouldn't be a problem to close the form "Main Menu" and reopen
it
but I don't know how to refer to all forms to close them.
I have tried the response to the question "Check and close all forms
except
the Switchboard" but the code didn't work. I do not have a Switchboard,
just
a standard form called "Main Menu". Is this the problem?
I am using Access 2003 and would be most appreciative of any help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top