Closing All Forms

  • Thread starter Thread starter Kevin S.
  • Start date Start date
K

Kevin S.

I earlier asked how to make a listbox that shows all open forms. I completed
this successfully using code that just makes a value list. By the way, does
anyone know what the size limit for a listbox value list is?

After the list of open forms is loaded, there is a close all button.
However, the code only closes every other form. (must be run more than once
to close all forms). Please help!

-------
Dim frm As Form

For Each frm In Application.Forms
If IsFormLoaded(frm.Name) <> 0 And Not frm.Name = Me.Name Then
DoCmd.Close acForm, frm.Name
End If
Next frm
-------

Am I doing anything wrong?
Thanks for the help

-Kevin
 
When you enumerate the members of a collection, and you delete a member from
within that loop, this can affect the successful enumeration of the
remaining members of that collection. When you close a form, you are
effectively deleting it from the Forms collection. So that is probably what
is happening here.

Try this instead, & see if it works.

(untested)

while forms.count > 1
if forms(1).name <> me.name then
DoCmd.Close acForm, forms(1).name
end if
wend

Just check that the first member of the forms collection is forms(1), not
forms(0). (I don't have Access here to check.) If it is forms(0), just
change forms(1) to forms(0) in two places above.

HTH,
TC
 
Back
Top