Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IS THERE AN MS ACCESS "CLOSE ALL FORMS" MACRO OR CODE COMMAND? 3

Status
Not open for further replies.

Hiccup

Programmer
Jan 15, 2003
266
US
Anyone know of a macro command or VBA code in Access 97 or 2000 that will close all open Forms? My database has several menu-driven Forms and SubForms(literally hundreds.) Not knowing which path the user will take, several Forms are remaining open. I need to close all Forms that have been accessed and open the next Form they want to access via a Form Commmand Button.

Appreciate any ideas. Thanks!

Ed "Hiccup" Hicks
ed.hicks@wcg.com
 
I'd keep a Collection of form names in a code module. Every time you open a form, add its name to the collection. Every time it closes, remove that name from the collection. Have a function that (allowing for errors) will loop through the collection and close every form listed. Your only problem will be accurately maintaining the list of forms. --
Find common answers using Google Groups:

 
Or you could create a function like this

public sub CloseForms()

Dim frm As Form
Dim i As Integer

For i = Forms.Count - 1 To 0 Step -1
DoCmd.Close acForm, Forms(i).Name
Next i

end sub
 
The For...Next loop in this case will be faulty, because you loop through a collection and modify its elements on the fly...

Better check if there are forms open and close the first element in the collection until all forms have been closed.

Sub CloseAllForms()
While Forms.Count > 0
DoCmd.Close acForm, Forms(0).Name
Wend
End Sub


Good luck,

[pipe]
Daniel Vlas
Systems Consultant

 
I agree yours works (and is probably the better of the two). However, I've tested mine and it works. Can you tell me where the problem is?
 
Well, for many elements in the collection, you may reach the point where the collection is refreshed. If your code has not finished by that time, the element 100 will be renumbered to let's say 15 (because of the removed elements). But your counter will try to delete element 100), which will not exist (or will be another one), but element 15 will remain there, as the counter passed once through that point.
Trust me, it happens. Try to delete the emails in an Outlook folder and you'll see the difference.
That's why I suggested the change...

HTH [pipe]
Daniel Vlas
Systems Consultant

 
ok, that makes sense. I didn't know the collection got refreshed. I was working with only a few forms. Thanks for the info
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top