Tip: Closing All Forms the Easy Way

<< Click to Display Table of Contents >>

Navigation:  Smart Access 1996-2006 > Dec-2000 >

Tip: Closing All Forms the Easy Way

Chris Weber        

Sometimes, I need to close all of the forms currently open in Access. Here's the simplest method that I've come up with:

Function CloseAllForms() As Boolean
On Error Goto AtEnd
 
  Do While Forms.Count
    DoCmd.Close acForm, Forms(0).Name
  Loop
 
At End:
  CloseAllForms = (Forms.Count = 0)
 
End Function
 

The routine closes the first form in the Forms collection (the Forms collection holds references to every open form). With the first form closed, what was the second form in the collection becomes the first form and is, in turn, closed. Eventually, any open form will have to fall into the 0 position in the Forms collection and will be closed. When all of the forms are closed, the Count property of the Forms collection becomes equal to 0. Since VBA treats 0 as False, my Do While loop, which is checking Errors.Count, terminates.

The function returns True or False, depending on whether or not all of the forms were successfully closed. If any forms are left open after the loop terminates, for instance, the test Forms.Count = 0 in the last line will be False, and that value will be returned from the CloseAllForms function.

 

or read this article by Ken Getz Closing All Open Forms