No, simple Unload FormA should be sufficient unless special code prevents it. Such a code might be inside of Form_Unload event also. But I cannot decline some other possibilities.
Golom: I tried doing it the way you advised formb.show unload me (as it sounds the most logical), but after stepping through formb.show the precedence is given to that form and formA will not unload until I close formB. I had this code in a grid click event (not in frmA_unload).
Vladk: There is no code in either of the form unload events.
I thought an Unloading of a form would stop a form from executing afterwards. So shouldn't unload me close/exit FormA and prevent formb.show from executing.
It confuses me too but unloading a form removes it from the Forms collection and destroys it's visible representation. Code in the form that is currently executing will however, continue to run.
I suspect that it has to do with the fact that the form is a windows object that is created or destroyed. As such, it is distinct from the form's code that (I gather) exists independently of the windows object that contains it.
This will probably take a Strongm or Hypetia to properly explain.
First of all, I would like to clarify that there is no harm in unloading a form in its Load event. I do it at times when needed (including many examples I post here; thread222-1271566 being the most recent).
You can check it yourself. Start VB and try running the following code.
___
[tt]
Private Sub Form_Load()
Unload Me
End Sub[/tt]
___
The code runs fine and programs finishes as expected without any errors.
In your case, I suspect that some statement, following the Unload Me statement, is referring to a data member, or control of FormA. That statement causes the FormA to load again, which does not popup on screen but stays loaded in memory.
Carefully look through your code that after Unload Me and before End Sub you do not access any property, method, or control placed on the form. If you find such code, remove it, or move it above the Unload Me statement so that it is executed before the form is unloaded and does not cause the form to load again.
As long as you don't break this rule (i.e. access a public member property, method or control on the form after unloading it), you can place your unload statement anywhere, including Load or QueryUnload events. Best way to avoid this situation is to place the [tt]Unload Me[/tt] statement immediately above the [tt]End Sub[/tt] statement as mentioned above, or atleast move it closer to the end as much as possible.
Note that after issuing an unload statement, you can still do a lot of things, as far as those things have nothing to do with the unloaded form. See the following example.
___
[tt]
Private Sub Form_Load()
Unload Me 'form is unloaded here
'just do some stuff
'get file listing at Cim S As String
S = Dir$("C:\")
While Len(S)
Debug.Print S
S = Dir$
Wend
End Sub[/tt]
___
But if you refer to any public member of the form, after unload statement, it causes the form to load again and stay in memory. See the following example.
___
[tt]
Private Sub Form_Click()
Unload Me 'form is unloaded here
'again doing some stuff
'but this time related to Form1
Dim S As String
S = Caption 'accessing the 'Caption' property causes the form to load again!
End Sub[/tt]
___
See also thread222-712089 where we had a discussion on a similar issue.
All good advice. I'll add something a little different, that may also be helpful:
rds, most of the time when I actually see someone doing this, it's because there's some sort of temporary process that the app needs to get through (most often a login screen) before going on to its real work.
If this is in fact what you're up to, you may want to consider a couple of other ways to accomplish this:
1. Make your FormB the startup form, and in the process of loading it, show formA modally. Let FormA do its stuff and unload itself. FormB will now be the active form.
2. For even more control, start with a Sub Main. Show your temporary form modally. Once it's done with, show your next form and you're on your way.
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.