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.
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!
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!
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.