View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Bob Phillips Bob Phillips is offline
external usenet poster
 
Posts: 10,593
Default Private Textbox Exit Sub question...

Trap that exit using QueryClose and set a variable

Option Explicit

Private fExitQuietly As Boolean
Private Sub EmpNum_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If Not fExitQuietly Then
If EmpNum.Value = "" Then
MsgBox "Employee number field cannot be left blank." & vbNewLine
& _
"Please enter a valid number."
End If
End If

End Sub

Private Sub UserForm_ACtivate()
fExitQuietly = False
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
fExitQuietly = True
End If
End Sub


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



wrote in message
s.com...
Hello all,

I have a Private Exit Sub for a Textbox that forces a person to enter
information in this box FIRST, before clicking into other parts of the
form. Basically, it's an employee time tracking form, and I've done
it this way because after they enter their employee number to start
off, it then goes up against another spreadsheet to bring back how
many vacation hours they have accrued to use up for the year. So this
way, they have to enter their number first in order to show what they
have, otherwise they have no business clicking around in other fields
and yada yada yada

Anyway, the question/problem I have is this: Once the userform is
displayed, I have part of the "verification" checking to see if they
left that textbox blank, and if they did it's shows a MsgBox that
says,

"Employee number field cannot be left blank. Please enter a valid
number."

Which works just fine, but if they click the "X" in the corner of the
userform to exit, it closes the form (which is what it's supposed to
do), but then because they left the textbox blank, after the forms
closes it still shows the message box telling them that the employee
field cannot be left blank.

Is there a way in the Private Exit Sub to specify, that IF the person
is "exiting" the textbox by clicking on the "X" to close, then not to
display the MsgBox?

Something like:

Private Sub EmpNum_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If EmpNum.Value = "" AND (Close Button is Clicked) Then
(Close UserForm without MsgBox)
Else
MsgBox "Employee number field cannot be left blank. Please
enter a valid number."
End If

End Sub