Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close Forms 1

Status
Not open for further replies.

007MCSE

IS-IT--Management
Jan 3, 2003
51
US
Is there an Unload command to close all open forms in one go?

Any help would be appreciated
 

Try ...
[tt]
Dim F As Form

For Each F In Forms
Unload F
Next
[/tt]

Good Luck

 
That works fine on my main form, but on any of my secondary forms ,It works the first time. then the second time I get the following errors:-

Runtime error 402
Must Close or hide topmost model form first.
 
You might try this:

Dim F As Form

On Error Resume Next

Do While Forms.Count > 0
For Each F In Forms
Unload F
Next
Loop

On Error Goto 0

If I'm right, this would skip over any forms that gave the error, and close any forms that it could close, then it would start over again trying to close forms until it got all of them.

Robert
 
You can also try the following,

For i = Forms.count - 1 To 0 Step -1
Unload Forms(i)
Next l_intCount

This unloads the forms in the reverse order of when they were loaded.
Thanks and Good Luck!

zemp
 
Hi Vampire

That didn't work, it still came back with the error
 
Try HIDE to end the modal form. This will cause it to start executing the code after the Show vbModal. I have no idea what will happen/

Dim F As Form

For Each F In Forms
F.hide
Next
For Each F In Forms
Unload F
Next

Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Hi Zemp

That didn't work either. I got the following
"Compile Error
Invalid next control variable referance" @ Next l_intCount

 
Hi John

That didn't work either, I got the

"Runtime error 402
Must Close or hide topmost model form first"

error again
 
007MCSE, I forgot to change a variable, the 'l_intCount' should be 'i' or you can just remove it.

For i = Forms.count - 1 To 0 Step -1
Unload Forms(i)
Next Thanks and Good Luck!

zemp
 
Hi Zemp

That works once then I get the following error

"Run-time error9
subscript out of range"

Its at Unload Forms(I)when you debug
 
Why is the code running twice? I assumed, perhaps falsly, that you were looking for a way to close all forms properly before shutting down the program.

If that is not the case then you will have to leave at least one form loaded. Add an if statement inside the loop to make sure that you are leaving the form you want open. Thanks and Good Luck!

zemp
 
Hi Zemp

It works fine in the main form. But its the subforms.
the subforms are forms for setting settings for the program.

One of the forms sets colors within the main form.

If you select that subform a second time to change the colors say at a later date then you get the error.

There is a routine to reload the main form and that works fine.

The problem is when you close any of the subforms a second time.

Gary
 
When you are working with 'subform' or configuration forms you should load it only when you need it and unload it as soon as you are done. The settings can be saved in a database or the registry. You can even set the formn to nothing to make sure it is destroyed.

In a close button I would place

unload form2

in the query unload event, or terminate event I would place

Set Form2 = nothing.

As I said before, the code example I posted was to unload all loaded forms before shutting down the program. Thanks and Good Luck!

zemp
 
Thats what I did at first.
I just had Unload and the form name under the command buttons "save" and "quit" and I started getting

Runtime error 402
Must Close or hide topmost model form first.

And thats why I posted this original thread because it didn't make any sense.

Thats all I wanted to do was close the forms.

The main form stays open all the time, until you exit the program and then that closes fine.
 
I was thinking, Is this problem caused by the way I'm opening the forms?

Public Sub Colors_Click() 'openform.LEDColors'
Out PortAddress, &H0
Dim frmLedColours As LEDColors
Set frmLedColours = New LEDColors
frmLedColours.Show vbModal, Me
End Sub
 
What is the definition of LEDColors. When you execute the statement Set frmLedColours = New LEDColors you are instantiating frmLedColours as an object of type LEDColors. The key element is what else, besides being a form, is an LEDColors object.

It may be that there are other objects and/or references within the LEDColors object which have to be cleared and/or released before the form can be unloaded. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Yes there is. Any new data entered on the form is saved to a file. Once the data is saved to the file then the File is closed. Then the close form is initiated.

Code for LEDcolors Form is:-

Private Sub Form_Load()
Dim I As Integer
For I = 0 To 7
Combo1(I).AddItem "Blue"
Combo1(I).AddItem "Clear"
Combo1(I).AddItem "Green"
Combo1(I).AddItem "MultiColored"
Combo1(I).AddItem "Orange"
Combo1(I).AddItem "Purple"
Combo1(I).AddItem "Red"
Combo1(I).AddItem "Yellow"
Next I
End Sub

Private Sub Quit_Click()
Call PPortLC1.Close_Forms
Dim frmPPortLC1 As PPortLC1
Set frmPPortLC1 = New PPortLC1
frmPPortLC1.Show vbModal, Me
End Sub

Private Sub Save_Click()
Open "c:\Program Files\PPortLC1\Data\LED.ini" For Output As #2
LED1 = Combo1(0)
LED2 = Combo1(1)
LED3 = Combo1(2)
LED4 = Combo1(3)
LED5 = Combo1(4)
LED6 = Combo1(5)
LED7 = Combo1(6)
LED8 = Combo1(7)
Write #2, LED1, LED2, LED3, LED4, LED5, LED6; LED7, LED8
Close #2

Call PPortLC1.Close_Forms


Dim frmPPortLC1 As PPortLC1
Set frmPPortLC1 = New PPortLC1
frmPPortLC1.Show vbModal, Me

End Sub

The PPortLC1 form is the main form.

The code for Call PPortLC1.Close_Forms is:-

Public Sub Close_Forms()
For I = Forms.Count - 1 To 0 Step -1
Unload Forms(I)
Next
End Sub
 
So, what you are doing is this ( please correct me if I am wrong ):

1: Your main form ( PPortLC1 ) opens your second form ( LEDcolors )

2: After selections are made on your second form, you click a button that saves the data and quits, or a button that just quits the second form.

3: This calls a sub in your main form, that closes ALL forms.

4: You then recreate a new main form( PPortLC1 ) and show it, using the "Me" keyword.

frmPPortLC1.Show vbModal, Me

The problem here, is that there is no "Me" anymore. All of your forms have been unloaded by calling:

PPortLC1.Close_Forms

a few lines before, so there is no "Me" to reference to.

I don't know why you are going through such a roundabout way of opening forms, but if you get rid of the "Me" at the end of that line, you might fix your problem.

Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top