[AccessD] Close All Forms

Heenan, Lambert Lambert.Heenan at AIG.com
Fri Aug 19 16:14:02 CDT 2005


It's normal practice to go in reverse when destroying items in a collection.
Here's why...

Say there are three forms open. With this code

For i = Forms.Count - 1 To 0 Step -1

On the first time through Forms.Count - 1 is evaluated as 2, and I is set to
2.

Then DoCmd.Close acForm, Forms(i).Name will close some form in the
collection (which is closed is unspecified as collections are not sorted).

Next time round the loop i is set to 1 and another form is closed. Etc. etc.

But if we try doing this in a 'forward' direction we have...

           For i = 0 to Forms.Count - 1 
                If Forms(i).Name <> "frmMainMenu" Then
                    DoCmd.Close acForm, Forms(i).Name
                End If
            Next i

First time round Count = 3, i is set to 0 and "forms(0)" closes.
Next time round Count = 2, i = 1 and "forms(1)" closes
Next time round Count = 1, i = 2 and "Forms(2).Name" generates an error
2456, "The number you user to refer to the form is invalid", which is the
collection's equivalent of "Subscript out of range"

The For loop is optimized by the compiler so that the termination condition
(Forms.Count-1) is only evaluated once. That's why you do it in reverse. 


None of this explains why Barbara is seeing Access crash though. Her code is
totally legitimate. It's not even anything to do with the fact that the
'Function' does not return a value. I'm sure she's set it up like that so it
can be called from an AutoKey macro.

So we are left with the possibility that the applications p-code is corrupt.
Time to back it up, then decompile and compact. With any luck that will fix
it. If not, import all the objects into a new database.

Lambert

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Charlotte Foust
Sent: Friday, August 19, 2005 4:36 PM
To: Access Developers discussion and problem solving
Subject: RE: [AccessD] Close All Forms


Is there any particular reason you're using a reverse step in this? Have you
tried it like this:

           For i = 0 to Forms.Count - 1 
                If Forms(i).Name <> "frmMainMenu" Then
                    DoCmd.Close acForm, Forms(i).Name
                End If
            Next i

Does that also bail out on End Function?

Charlotte Foust


-----Original Message-----
From: Barbara Ryan [mailto:BarbaraRyan at cox.net] 
Sent: Friday, August 19, 2005 11:52 AM
To: Access List
Subject: [AccessD] Close All Forms


I have used the following code successfully in Access 97 to close all forms
when a specific function key is pressed.  However, in Access 2002, this code
causes the error box to appear (with the 2 options to send an error report
to Microsoft or not) and then closes Access.  Does anyone know why?

    Function CloseAllForms()
        Dim i As Integer
        ' Loop through the collection of open forms and close them (except
for the main menu)
        If Forms.Count > 0 Then
            For i = Forms.Count - 1 To 0 Step -1
                If Forms(i).Name <> "frmMainMenu" Then
                    DoCmd.Close acForm, Forms(i).Name
                End If
            Next i
        End If
    End Function

Thanks,
Barb Ryan

-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com
-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com



More information about the AccessD mailing list