If I need to determine whether a user cancelled input from a userform
(either with a Cancel command button or the "X" on the form's caption
bar), I create a public read-only property called UserCancelled and
test that in the code that launched the form. E.g., in your form's
code module:
Private bUserCancelled As Boolean
Public Property Get UserCancelled() As Boolean
UserCancelled = bUserCancelled
End Property
Private Sub btnCancel_Click()
bUserCancelled = True
Me.Hide
End Sub
Private Sub btnOK_Click()
bUserCancelled = False
Me.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
bUserCancelled = True
Me.Hide
End Sub
Then, in a regular code module, use code like
Sub AAA()
UserForm1.Show
If UserForm1.UserCancelled = True Then
Debug.Print "Cancel"
Else
Debug.Print "OK"
End If
Unload UserForm1
End Sub
In this code, you don't Unload the form from within the form's code
module. Instead, just Hide the form. When you Unload the form, it is
dumped from memory and you can't get the values of the controls on the
form. When you Hide a form, you simply make it invisible but it
remains loaded in memory and its properties and controls remain
accessible.
Private Sub CommandButton2_Click()
'strEnterName = ""
Unload Me
frmEnterName.Hide
End Sub
Get rid of the Unload Me line of code.
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)