Avoiding the form 'hidden global variable' has me befuddled
Posted on 2003-02-26
Hello Experts. Thanks for your interest in my question.
I've got an executable that uses a 'switchboard' sort of main form to open other forms. Blissful in my ignorance, it seemed as though everything was working fine using the following 'switchboard' code to open other forms (The IsLoaded("x") proc loops through the forms collection trying to find one whose name is "x"):
If IsLoaded("form_name") Then
form_name.Visible = True
form_name.WindowState = vbNormal
Then, one day, my code exploded when a private, form module-level variant-type variable wasn't empty when I expected it to be. The error appeared the second and subsequent times I opened the form. I figured out that the variable is never out-of-scope because the form_terminate event never fires because the hidden global variable I inadvertantly created with 'form_name.Show' is still alive. The solution is simple if I'm willing to open the forms modally:
Dim f as Form
Set f = New form_name
Set f = Nothing
The form_terminate event fires and the module level variables go out-of-scope. If the user re-opens the form they're not haunted by 'ghosts of form variables past'. But this approach is unacceptable because users shouldn't have to close the widgets form just so they can open the doodads form. Cascading modal forms (modal widgets opens modal doodads opens modal etc) is also unacceptable.
My goal is to restore the instance of the form that the user had minimized and forgotten rather than create a new instance; but when the form is closed the module-level variables need to be destroyed. So, hands on buzzers, how can I get form x to restore form y if it's already open, open it if it's not, and form y's module-level variables to go out-of-scope when the user closes it?
I'm working around the problem by resetting module-level variables to their original values in the QueryUnload event, but that's cheesy and I want to keep the cheese I'm aware of to an absolute minimum.
Thanks for your help.