W
WLMPilot
I have a userform that is used to order supplies. Each time through, the
stock item and max stock level is displayed. User enters "Stock On Hand"
(Textbox1). If valid entry is made, Textbox2 (order quantiy) = Max stock
level - Textbox1.
The following macro (see below) executes on Keycode 9 or 13 (tab or enter)
for Textbox1. I try to catch errors of negative number, character entry, or
blank field.
PROBLEM SO FAR: I enter a null value to test. It catches the error and
then goes to "BADENTRY". However, the setfocus does not occur for Textbox1.
The focus is placed on Textbox2. I need it back in Textbox1 so user can
re-enter the correct numerical data.
Any Ideas?
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 13 Or KeyCode = 9 Then
Dim Msg As String
Dim onhand, ErrorNum As Integer
'***********************************************
'Error Check Section
'Correct Entry >= 0
'Check for blank entry
If TextBox1 = "" Then
GoTo BadEntry
End If
'Test for negative number
onhand = Str(TextBox1)
On Error GoTo BadEntry
If onhand < 0 Then
GoTo BadEntry
End If
'***********************************************
'Data is Correct. Proceed
TextBox2 = Str(TextBox4) - Str(TextBox1)
TextBox1.BackColor = vbWhite
CommandButton4.Visible = True
CommandButton1.SetFocus
GoTo Closeout
'***********************************************
BadEntry:
Msg = "INVALID ENTRY" & vbNewLine
Msg = Msg & "Please enter a number" & vbNewLine
Msg = Msg & "greater than or equal to zero."
MsgBox Msg
TextBox1 = ""
TextBox1.SetFocus
Closeout:
End If
End Sub
stock item and max stock level is displayed. User enters "Stock On Hand"
(Textbox1). If valid entry is made, Textbox2 (order quantiy) = Max stock
level - Textbox1.
The following macro (see below) executes on Keycode 9 or 13 (tab or enter)
for Textbox1. I try to catch errors of negative number, character entry, or
blank field.
PROBLEM SO FAR: I enter a null value to test. It catches the error and
then goes to "BADENTRY". However, the setfocus does not occur for Textbox1.
The focus is placed on Textbox2. I need it back in Textbox1 so user can
re-enter the correct numerical data.
Any Ideas?
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 13 Or KeyCode = 9 Then
Dim Msg As String
Dim onhand, ErrorNum As Integer
'***********************************************
'Error Check Section
'Correct Entry >= 0
'Check for blank entry
If TextBox1 = "" Then
GoTo BadEntry
End If
'Test for negative number
onhand = Str(TextBox1)
On Error GoTo BadEntry
If onhand < 0 Then
GoTo BadEntry
End If
'***********************************************
'Data is Correct. Proceed
TextBox2 = Str(TextBox4) - Str(TextBox1)
TextBox1.BackColor = vbWhite
CommandButton4.Visible = True
CommandButton1.SetFocus
GoTo Closeout
'***********************************************
BadEntry:
Msg = "INVALID ENTRY" & vbNewLine
Msg = Msg & "Please enter a number" & vbNewLine
Msg = Msg & "greater than or equal to zero."
MsgBox Msg
TextBox1 = ""
TextBox1.SetFocus
Closeout:
End If
End Sub