Closing All Forms

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
 
T

TC

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top