M
matt
I have a user form (i.e. frmGrade) that has a number of text boxes in
it (i.e. properties window/name = txtExpDesignA, txtExpDesignB,
txtExpDesignC...). The text boxes are set to 0 (i.e. properties winow/
value = 0) and the textboxes can take on a value between 0 and 5. I'm
using an event (for when the user switches text boxes to enter
additional information) to call a function that validates whether or
not the text box value is indeed between 0 and 5. If the value is NOT
between 0 and 5 then I want the program to "reselect" the textbox that
has the error in it.
For example, assume that the first text box is txtExpDesignA and when
you hit <tab> the cursor moves to txtExpDesignB, and when you hit
<tab> again the cursor moves you to txtExpDesignC, and so on.
If I enter 6 in txtExpDesignA (which is outside of the values 0 to 5)
and hit <tab> the program will tell you that you entered an incorrect
value for txtExpDesignA. As a result, I want the cursor to stay in
txtExpDesignA and NOT move to txtExpDesignB on the change. In the
code below you will notice that I'm using
frmGrade.txtExpDesignA.SetFocus, but this does not seem to "reselect"
txtExpDesignA because the program just "tabs" to the next text box
(i.e. txtExpDesignB); however, if I use
frmGrade.txtExpDesignC.SetFocus, the program will skip over
txtExpDesignB and select txtExpDesignC just fine. Can someone clue me
in on how to "reselect" txtExpDesignA on the event.
An added question would be this: Is there a way to highlight all the
contents of the text box on using the .SetFocus method rather than
just moving the cursor? (i.e. if I do txtExpDesignC.SetFocus, the
cursor blinks at the end of the text...is there a way to have it
highlight the entire text just like the text is highlighted when you
hit <tab>?)
Thanks in advance,
Matt
Option Explicit
Dim score As Boolean
Private Sub txtExpDesignA_AfterUpdate() 'Change
score = validate5(frmGrade.txtExpDesignA.Value)
If score = False Then
MsgBox ("You input an incorrect value in ""A.""" & Chr(13) _
& "The value must be a number and <= 5.")
'frmGrade.txtExpDesignC.SetFocus
frmGrade.txtExpDesignA.SetFocus
End If
End Sub
Private Function validate5(number) As Boolean
If (IsNumeric(number) = True) And (number <= 5) And (number > 0) Then
validate5 = True
Else
validate5 = False
End If
End Function
it (i.e. properties window/name = txtExpDesignA, txtExpDesignB,
txtExpDesignC...). The text boxes are set to 0 (i.e. properties winow/
value = 0) and the textboxes can take on a value between 0 and 5. I'm
using an event (for when the user switches text boxes to enter
additional information) to call a function that validates whether or
not the text box value is indeed between 0 and 5. If the value is NOT
between 0 and 5 then I want the program to "reselect" the textbox that
has the error in it.
For example, assume that the first text box is txtExpDesignA and when
you hit <tab> the cursor moves to txtExpDesignB, and when you hit
<tab> again the cursor moves you to txtExpDesignC, and so on.
If I enter 6 in txtExpDesignA (which is outside of the values 0 to 5)
and hit <tab> the program will tell you that you entered an incorrect
value for txtExpDesignA. As a result, I want the cursor to stay in
txtExpDesignA and NOT move to txtExpDesignB on the change. In the
code below you will notice that I'm using
frmGrade.txtExpDesignA.SetFocus, but this does not seem to "reselect"
txtExpDesignA because the program just "tabs" to the next text box
(i.e. txtExpDesignB); however, if I use
frmGrade.txtExpDesignC.SetFocus, the program will skip over
txtExpDesignB and select txtExpDesignC just fine. Can someone clue me
in on how to "reselect" txtExpDesignA on the event.
An added question would be this: Is there a way to highlight all the
contents of the text box on using the .SetFocus method rather than
just moving the cursor? (i.e. if I do txtExpDesignC.SetFocus, the
cursor blinks at the end of the text...is there a way to have it
highlight the entire text just like the text is highlighted when you
hit <tab>?)
Thanks in advance,
Matt
Option Explicit
Dim score As Boolean
Private Sub txtExpDesignA_AfterUpdate() 'Change
score = validate5(frmGrade.txtExpDesignA.Value)
If score = False Then
MsgBox ("You input an incorrect value in ""A.""" & Chr(13) _
& "The value must be a number and <= 5.")
'frmGrade.txtExpDesignC.SetFocus
frmGrade.txtExpDesignA.SetFocus
End If
End Sub
Private Function validate5(number) As Boolean
If (IsNumeric(number) = True) And (number <= 5) And (number > 0) Then
validate5 = True
Else
validate5 = False
End If
End Function