Closing all open forms in VB

MarionD

Registered User.
Local time
Today, 20:00
Joined
Oct 10, 2000
Messages
425
Hi all,
I have a Front End Db which can be linked to different Back End DB's. For this I have VB code which asks the user to select the Back-end DB then relinks the tables. This works fine, but I want to close all the open forms when a different Back end db is linked.
How can I Find out which forms are open and close them?[/FONT] Would I have to loop through all the forms? If so How?

Thanks a lot
Marion :confused:
 
I believe there is a Forms collection with a count object which only holds forms loaded or open (can't remember which).

Loop from the max count to 1 and close each one.
If it's VBA - docmd.close acform, forms(loopcounter).name
If it's VB - forms(loopcounter).hide or unload (forms(loopcounter))

Have an experiment and post up the working code :)


Vince
 
Hi Vince,

Thanks for the reply. I have experimented and have half the solution I'd like.
If I use this code, it works fine but leaves some forms open. It seems that after closing a form the forms collection moves on one, thus skipping a few forms. It seems there must be some way of refreshing the forms collection?

For Each frm In Application.Forms
If frm.Name <> "Startform" Then
DoCmd.Close acForm, frm.Name
End If

If I enclose the above in a count loop it closes all the forms, but performs the 'for each' x number of times - correct result but not perfect coding!

x = Application.Forms.Count
For i = 1 To x
For Each frm In Application.Forms
If frm.Name <> "Startform" Then
DoCmd.Close acForm, frm.Name
End If
Next frm
Next i

Thanks for any suggestions!
Marion
 
Hi,

Almost there. There problem with looping upwards (through each form or from 1 to x forms) is that as you close a form the rest slide back and the looping pointer doesn't.

[vbcode]
for lngLoop = forms.count to 1 step -1
If forms(lngLoop).Name <> "Startform" Then
DoCmd.Close acForm, forms(lngLoop).Name
End If
Next frm
[/vbcode]

Try the above... :)


Vince
 
Hi Vince,

Thanks for the advice. Still one problem though.
If forms.count=5 then the runtime error 2456 appears (the number with which you are referring to the form is invalid) (My translation from German! :o )

If I change the lngloop to start from the beginning:
for lngloop=1 to forms.count step 1
then it works fine for 2 forms but when lngloop = 3 then the same error message appears.

Any ideas?
Thanks for your time - I really appreciate it

Marion
 
Doesn't this work?

Dim frm As Form
For Each frm In Forms
If frm.Name <> "Startform" Then
DoCmd.Close acForm, frm.Name
End If
Next
 
Hi Rich,

Yes it works - BUT if for example 5 forms are open, it only closes 3 and leaves 2 open. As Vince said ,"There problem with looping upwards (through each form or from 1 to x forms) is that as you close a form the rest slide back and the looping pointer doesn't."

Thanks for the time!
 

Users who are viewing this thread

Back
Top Bottom