Close all open and hidden forms with one button?

sgtSortor

Registered User.
Local time
Yesterday, 16:24
Joined
Mar 23, 2008
Messages
31
Is there a quick and easy way to Close all open and hidden forms with one button?
 
See if this will work:

Code:
Dim frm As Object
For Each frm In CurrentProject.AllForms
    If frm.IsLoaded Then
        DoCmd.Close acForm, frm.Name
    End If
Next frm
 
And since Bob is not logged in I'll point out that it appears that it is his method which is flawed - Sometimes it will close all of the forms and sometimes it will not not. I think it has to do with the way the forms are enumerated - :)

Would someone be so kind as to run both code samples and confirm which code works? My code works well on my machine. It may be just a difference in our MS Access versions - His code may work well in what he uses and vice versa. I'm using 2003 - ?
 
Ken,

Both worked for me in Access 2003 and XP Home. I did them both times...yours, Bobs, yours and Bobs
 
Hum... I created three forms and put my code behind a button on one of them, one button with his code on another form and the third form was just blank. Then I opened all three and hit my button and it always closed them all. Then I opened them all and tried his code and some times all of them closed but most of the time it left one or more forms open. I'll point out that I changed the order in which I opened them and could not find a pattern as to which form(s) seemed to stay open when I run his code... ???
 
Here it is. Try it opening the forms in different order and try closing them all using your method- ?

Am I missing something (Been there :p)
 

Attachments

Okay, I just did another test and opened 4 forms and I admit that mine did leave one open when I did one of the tests. I apologize. I must have just been lucky when using it.

You had me thinking I was going zonkers - :p

Mike - Did the act up on your xp machne - ?
 
Ken,

When I start my DB a macro opens 9 forms and I put the code behind a text box on the form that is the last to open.
 
Mine closed all 9 forms with both codes. I just tried it again.

I don't know if it makes any difference.....but the 9 forms opened are 9 that stay open. However, in the start up process a couple of other forms are opened (used for SetValues) and closed.

As a side note I hope you blokes don't mind if I stick with my macro that closes the 9 forms:D
 
If my memory serves me right, we'd never be able to talk you out it anyway - :p
 
Ken

Update.

I just tried Bobs on a frontend version of the DB and put the code on a textbox, button and label. In each case it only closed the form that had the button, label and textbox.

Then put your code behind the label, textbox and button and all forms closed.
 
Wish someone had a link to a good read on collections, etc
 

Users who are viewing this thread

Back
Top Bottom