I have a routine that, at one step, calls a form and shows it modal. When the user closes the form the routine continues. However, upon closing the form I want to know if it performed its assigned function- if it failed then I need to stop the routine. However, since the form was opened modally and then closed and its memory unloaded at the end, I don't see any way to get any information about what it did. I could have the form trigger a global boolean variable but that does not seem the correct way. There has to be a simple solution, I just can't see it, please help...

Okay, I found something that works but I am still just as confused. In my parent sub I have a variable to the form. I also have given the form a public boolean variable, ConnSuccess, so it goes something like (pseudocode):

Sub CreateConn
dim fmConn1 as fmConn

set fmConn1 = new fmConn

fmConn1.Show vbModal

If fmConn1.ConnSuccess then
(etc etc)

So what is the meaning of when the user closes fmConn and it supposedly unloads from memory? Because the fact that I can still read variables and properties inside this form proves that it is still there? Does it only truly go away when I set fmConn1 = Nothing?

Hi,

Global variable or Properties can be used as your suggestion.
Assume you have Ok and Cancel button in your modal form.
In the Modal form, user can click Ok button for approval and for disapproval they can click Cancel Button or Close Button.

When Close button is Clicked the form Unloads from Memory. so u can only receive the default values. At this

When Cancel button is clicked you should hide the form instead unload from memory and Make the Variable False(default value).

When Ok button is clicked here also you should hide the form instead unload from memory and Make the Variable True.

Finally you can unload the form after shown modally.

Ex
Assume this is login form(frmLogin) Cotains Ok and Cancel button

Public LoginSucceeded As Boolean

Private Sub cmdCancel_Click()
    'set the global var to false to denote a failed
    LoginSucceeded = False
    Me.Hide
End Sub

Private Sub cmdOK_Click()
    'check for Login
    If DoLogin() Then
        LoginSucceeded = True
        Me.Hide
    Else
        'Invalid User Name or Password
    End If
End Sub

Then you use this login form may be like

Private Sub cmdLogin_Click()
   Dim fLogin As frmLogin
   Set fLogin = New frmLogin
   
   fLogin.Show vbModal
   If fLogin.LoginSucceeded Then
      'Here Success of Login
   Else
      'Failure of Login
   End If
   'Unload from Memory
   Unload fLogin
End Sub

Thanks, I can see how it works now.

Is unloading a form variable in the code the same thing as setting it to Nothing?

I think it's NO. It must be dispose by setting it to Nothing. Unloading the form varaible means you're just closing the form and since you did not dispose the form's variable ergo the variable was still there.

Unloading the form varaible means you're just closing the form

I guess then my question is what exactly constitutes "closing the form". I thought unloading meant clearing from memory, but as my above code demonstrated, my user-created properties of the form class are still there even after I have unloaded. Maybe it just clears the memory associated with all the properties of form's controls...

I admit this is getting a bit away from the original topic, as the above examples already solved my problem. I'm just trying to get a fuller understanding what these commands such as "unload" are really doing.

Hi,
When we use Unload statement the form is getting unload (visible=false) from memory and not destroyed as said by jireh. Then the form is removed from the Forms collection. It is not actually destroyed, until all references to it are set to Nothing. When u call a method or properties of unloaded form, it is again reinitialize all the components so it lost all the previous values

Unload statement fires Query_Unload and Unload events. If you use End statement, it wont fire.

Okay, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.