Problems Unloading a Form
WinXP/VB6. I can't seem to get 'Unload Me' to work when my App is processing code beneath a command button. For example, the user clicks a command button on Form1 whose code calls several related SUBs:
Private Sub Command1_Click()
Call Sub1()
Call Sub2()
(in sub 2)...............Load Form2
.........................Unload Me
.........................(control continues on?)
Call Sub3()
End Sub
I want the option to load a new form (Form2) from the Sub2() code in Form1 and immediately stop all processing in Form1 without another line of code being read. I THOUGHT I could accomplish this by simply unloading Form1 when I reached this point, but I have found that Form1 will continue to process code (past 'Unload Me') until it reaches the end of all code beneath the Command1 button (apparently reloading the form to do so).
I have the sick feeling I'm using bad coding practices. Can someone confirm this? Is there a proper way to stop the current form from processing another line of code from a stated point and unload it immediately? Do I need to clear some reference somewhere?
Frustrated ... any help, advice, suggestions appreciated.
Shannon
[1263 byte] By [
Shannon] at [2007-11-11 10:22:11]

# 1 Re: Problems Unloading a Form
I don't think you can use Unload to instantly stop all processing in a form. I would create a form-level flag (e.g., UnloadMe) and set it to True when you want to unload the form. Then check the value of the flag after each statement and only execute the next statement if the flag is False:
Option Explicit
Private UnloadMe As Boolean
Private Sub cmdButton_Click()
Call Sub1
If Not UnloadMe Then Call Sub2
If Not UnloadMe Then Call Sub3
If UnloadMe Then Unload Me
End Sub
# 3 Re: Problems Unloading a Form
Okay, then Phil, can I assume then that all active SUBs must then complete their code before a form will be unloaded, even after the 'Unload Me' command has been issued?
And maybe more important, does this mean I am not necessarily using sloppy coding practices, I likely just need to move things around a bit?
It's been a long day, I think I'm mumbling ... thanks for help (and you too Marco).
Shannon
# 5 Re: Problems Unloading a Form
Just open the form as acDialog,
This property will stop the exec. of the current sub, till form2 is loaded.