Forms:Close All

Wednesday, January 01, 2003 22:03:21
home

One way to quickly close all forms is to create a module with the following code, which you can then call:

Function closeallfrms()
Dim intF As Integer
' The OKToClose = True command is here to allow the form to close, as forms
' are prevented from closing via the Disable Form Closing, disable_form_database_closing.html
' If you don't have that, you can remove that command. http://www.dbases.net
OKToClose = True
    For intF = Forms.Count - 1 To 0 Step -1
                DoCmd.Close acForm, Forms(intF).name, acSaveNo
    Next intF
End Function

I derived this code from the newsgroup comp.databases.ms-access, dated June 4th 2002 entitled "Close all forms except one." I have included the text of this thread below:


Message 1 in thread
From: PMah (duh_ster@hotmail.com)
Subject: Close all forms except one

View this article only
Newsgroups: comp.databases.ms-access
Date: 2002-06-04 09:00:00 PST

Hello,

I am designing the User Interface forms for my database. I have 1 form which is the Main Menu. As the User navigates through the different forms, I wish to have a button on each form which will return the User to the Main Menu and close all forms except the Main Menu. How can I do this? If this is not possible, can I close ALL windows and then just open the Main Menu back up?

Thanks!
Pete
Message 2 in thread
From: Olaf Rabbachin (Olaf.Rabbachin@IntuiDev.com) Subject: Re: Close all forms except one

View this article only
Newsgroups: comp.databases.ms-access
Date: 2002-06-04 09:10:02 PST

Hi Pete,

> I am designing the User Interface forms for my database. I have 1 > form which is the Main Menu. As the User navigates through the > different forms, I wish to have a button on each form which will > return the User to the Main Menu and close all forms except the Main > Menu. How can I do this? If this is not possible, can I close ALL > windows and then just open the Main Menu back up?

try something like this ...

sub closeForms(strExceptThisOne as string) dim frm as form

for each frm in application.forms
if frm.Name<>strExceptThisOne then _
docmd.close acform,frm.name,acsaveno next frm
end sub

... and calling passing the name of your main form.

Cheers,
Olaf
Message 3 in thread
From: Marshall Barton (marshbarton@mindspring.com) Subject: Re: Close all forms except one

View this article only
Newsgroups: comp.databases.ms-access
Date: 2002-06-04 09:46:02 PST

Olaf Rabbachin wrote:

>Hi Pete,
>
>> I am designing the User Interface forms for my database. I have 1 >> form which is the Main Menu. As the User navigates through the >> different forms, I wish to have a button on each form which will >> return the User to the Main Menu and close all forms except the Main >> Menu. How can I do this? If this is not possible, can I close ALL >> windows and then just open the Main Menu back up? >
>try something like this ...
>
>sub closeForms(strExceptThisOne as string) > dim frm as form
>
> for each frm in application.forms
> if frm.Name<>strExceptThisOne then _ > docmd.close acform,frm.name,acsaveno > next frm
>end sub
>
>... and calling passing the name of your main form.

Almost, but not quite. For Each will skip some entries since close will cause the Forms collection to be adjusted when an item is removed. Try this instead:

Sub CloseForms(strExceptThisOne As String) Dim intF As Integer

        For intF = Forms.Count - 1 To 0 Step -1
                If Forms(intF).Name <> strExceptThisOne Then _
                        DoCmd.Close acForm, Forms(intF).Name, acSaveNo
        Next intF

End Sub

--
Marsh