PC Review


Reply
Thread Tools Rate Thread

Close all open forms except 1

 
 
=?Utf-8?B?U2ltb24=?=
Guest
Posts: n/a
 
      20th May 2006
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
 
Reply With Quote
 
 
 
 
Douglas J. Steele
Guest
Posts: n/a
 
      20th May 2006
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

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)


"Simon" <(E-Mail Removed)> wrote in message
news:B7CC37AE-F0AF-470B-BB30-(E-Mail Removed)...
>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



 
Reply With Quote
 
Allen Browne
Guest
Posts: n/a
 
      20th May 2006
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.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Simon" <(E-Mail Removed)> wrote in message
news:B7CC37AE-F0AF-470B-BB30-(E-Mail Removed)...
>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



 
Reply With Quote
 
=?Utf-8?B?U2ltb24=?=
Guest
Posts: n/a
 
      20th May 2006
Thank you both very much.

"Allen Browne" wrote:

> 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.
> Tips for Access users - http://allenbrowne.com/tips.html
> Reply to group, rather than allenbrowne at mvps dot org.
>
> "Simon" <(E-Mail Removed)> wrote in message
> news:B7CC37AE-F0AF-470B-BB30-(E-Mail Removed)...
> >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

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Forms - how open and close????? grant@technologyworks.co.nz Microsoft Outlook Discussion 4 14th May 2008 09:41 PM
Forms: Buttons to open other forms and close button Veli Izzet Microsoft Access 3 23rd Aug 2005 07:44 AM
Open & Close forms postman Microsoft Access Form Coding 2 15th Mar 2005 07:14 PM
Can I close all open forms? =?Utf-8?B?U2hlaWxhIEQ=?= Microsoft Access 4 7th Oct 2004 04:35 PM
Close all open forms Mike Microsoft Access Forms 2 19th Aug 2004 05:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:35 PM.