Full Version: close all open forms at once
UtterAccess Forums > Microsoft® Access > Access Forms
I understand how to close a form using docmd.close, but is there a way via code to close ALL open forms at once?
Jack Cowley
Dim frm As Form
Dim a As Integer
For a = 1 To Forms.Count + 1
For Each frm In Forms
If frm.Name = Me.Name Then
'Do nothing
DoCmd.Close acForm, frm.Name
End If
Next frm
Next a
I was close!
Thanks Jack!
Just to offer a subtle alternative - when removing members from a collection, it's a common practice to iterate backwards through the collection. (This is avoid members getting skipped when their ordinal position in the collection changes due to the shrinking collection ;-)
o you end up with something like
Dim intI As Integer
For intI = Forms.Count - 1 to 0 Step -1
    DoCmd.Close acForm, Forms(intI).Name

Bear in mind that though this doesn't explicitly take into consideration multiple instances of the same form (if you happen to have implemented such functionality) it should still get through them all by sheer persistence ;-)
Jack Cowley
What I did 5 years ago with 12 lines of code you are always able to reduce to 3 or 4 lines! Thanks Leigh!! Now I am replacing my old code with a newer, shorter version!
You can also do this:
But the following in a standard module:

Public Function CloseObject(strContainerName As String, intContainerType As Integer)
' Close open database objects.
Dim dbs As Database, ctr As Container
Dim intX As Integer
Set dbs = CurrentDb
Set ctr = dbs.Containers(strContainerName)
For intX = 0 To ctr.Documents.Count - 1
DoCmd.Close intContainerType, ctr.Documents(intX).Name
Next intX
End Function

Then put this wherever you want to close all forms:
CloseObject "Forms", acForm

It works for all types of Access objects.
QualityMan a.k.a George
Another one:
[color="blue"]While Forms.Count
   DoCmd.Close acForm, Forms(0).Name
Wend  [/color]
Jack Cowley
Anyone for 2 lines of code???
-----> :
Jack Cowley
I thought of that, but too late.... :(
Well it's the old adage really - fewer lines doesn't necessarily mean quicker execution... :-)
That said, if we're talking *lines* in the traditional writing sense...
Dim intI As Integer: For intI = Forms.Count - 1 To 0 Step -1: DoCmd.Close acForm, Forms(intI).Name: Next
That's one!
I'm tempted to throw a flag on that one! Indeed you are correct but it is piling on!
Actually that one line of code uses all the "tricks" that I'd point out.
I'm sure LPurvis would agree (and another poster pointed out) less lines doesn't mean quality. In fact, it is the other way (generally) - "you get what you pay for"! Here the advantage is that it is much easier to understand this code quickly if it is properly formatted:
  Dim intI As Integer
  For intI = Forms.Count - 1 To 0 Step -1
     DoCmd.Close acForm, Forms(intI).Name

Couple of things I'd like to (re-) emphasize. When closing objects you want to start from the "back" of the object collection. So as LPurvis used a "Step -1" to reference the "last" form from the total count of forms (minus 1 because the count is zero-based).
Moving backwards through a Collection prevents issues that arise when the form name and collection index are updated as forms are deleted.
Also, the DoCmd.Close is explicit in what gets closed - a form (acForm) with the proper name (Forms(intI).Name). If you do not explicitly specify what gets closed and just use DoCmd.Close then whatever object has the focus at the time will be closed. Could be the module you just wrote an hour's worth of code into without saving!
Forms.Count - 1 To 0 Step -1
This is preferred.
My post was in no way intended to be a serious one - as that was the direction the thread seemed to have taken to my mind (one of levity).
expressed that exact opinion that less *isn't* better in that same post.
(And of course - the other points earlier - where I offered my *real* solution :-)
From the first question…“I understand how to close a form using docmd.close, but is there a way via code to close ALL open forms at once?”
I think the real answer is… NO.
Anything done in a loop, be it forward or backward, is still done in sequence and therefore not ‘ALL at once’.
Application.Quit acQuitSaveAll
Would close the Forms in sequence and not ‘ALL at once’ simply because it is still under program control.
Now if we were allowed to switch the power off then one could assume that ‘ALL forms were closed at once’. But switching the power off does not appear to be an option under the original question i.e. ‘is there a way via code’.
Of course, if the code had control over an electrical contactor that was supplying power to the processor then it might be possible to get reasonably close to ‘ALL at once Form closure via code’.
This reply is largely stupid... but not completely stupid. wary.gif
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.