Hi there,
I am using Excel 2013 and am getting some strange behaviour when unloading a userform using the "Unload Me" statement. From what I understand the Unload command should remove the instance of the userform from memory. However even after the unload statement has executed, I am still able to access the userform and it's properties.
My module code:
And the userform code:
If I run the code and enter something in the userform textbox and then press the cancel button, the code correctly executes the unload command, but the "If Not (myFrm Is Nothing) Then" evaluates to true and the code is still able to set the strTextEntry value to the value of the userform text box, even though it should have been unloaded. Any thoughts?
Thanks!
I am using Excel 2013 and am getting some strange behaviour when unloading a userform using the "Unload Me" statement. From what I understand the Unload command should remove the instance of the userform from memory. However even after the unload statement has executed, I am still able to access the userform and it's properties.
My module code:
Code:
Option Explicit
Sub FormsTest()
Dim myFrm As frmTestForm
Set myFrm = New frmTestForm
Dim strTextEntry As String
Dim MsgBoxResult1 As VbMsgBoxResult
'Show form for user to enter new employee details
myFrm.Show
'If user clicks OK then instance will still exist so get the data from form
If Not (myFrm Is Nothing) Then
strTextEntry = myFrm.tbTextEntry.Value
MsgBoxResult1 = MsgBox("You have entered the following text: " & strTextEntry, vbOKOnly)
'If user clicks cancel then form will be unloaded (in "cmdCancel_Click()" procedure) and so instance won't exist
Else
MsgBoxResult1 = MsgBox("You cancelled the data entry", vbOKOnly)
End If
End Sub
And the userform code:
Code:
Option Explicit
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub cmdOK_Click()
Me.Hide
End Sub
If I run the code and enter something in the userform textbox and then press the cancel button, the code correctly executes the unload command, but the "If Not (myFrm Is Nothing) Then" evaluates to true and the code is still able to set the strTextEntry value to the value of the userform text box, even though it should have been unloaded. Any thoughts?
Thanks!