Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

text box rules

Status
Not open for further replies.

neoice

Programmer
Feb 15, 2003
110
0
0
GB
Hi Guys,

I need some more advise please. I am trying to set rules on a text box. Basically I do not want to allow anything other than the numbers 7 to 13 to be accepted. This works to a degree but fails it somebody choose back space or anything other than just type in a number. Ideally i would like it to check the txt box rules once the user has pressed tab or the return key. As I cannot see those options available I used the change sub.

Is it possible to check the contents of the txt box when a user presses tab or return?

Thanks your help is much appreciated.

Private Sub txtreg_Change()
If txtreg.Text < 7 Or txtreg.Text > 13 Then MsgBox &quot;Please type in a current Year&quot;: txtreg.Text = &quot;&quot;: txtreg.SetFocus: GoTo lp


cmbcredit.Enabled = True
lp:
End Sub
 

Use the TextBox_Validate event

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
This may help to some extent. May have to think about how your going to handle 10, 11, 12 and 13 though


Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = KeyCheck(KeyAscii, &quot;0123456789&quot;)
End Sub


Public Function KeyCheck(KeyIn As Integer, Optional allow As String = &quot;&quot;, Optional forbid As String = &quot;&quot;) As Integer
' Allow or Forbid Key Strokes
KeyCheck = KeyIn
If KeyIn = vbKeyBack Then Exit Function
If InStr(forbid, Chr$(KeyIn)) Or (allow <> &quot;&quot; And InStr(allow, Chr$(KeyIn)) = 0) Then
KeyCheck = 0
Beep
End If
End Function

Good Luck
 
I'd use something like this:

Private Sub Text1_LostFocus()
If Val(Text1.Text) < 7 Or Val(Text1.Text) > 13 Then
MsgBox &quot;This is an Error&quot;
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
End If
End Sub

It checks and validates on the LostFocus event.

Hope this helps,
Pan
 
Panthaur,
See the Validate event (as I suggested above). It fires before Lost_Focus and you can use the Cancel to prevent loss of focus.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
neoice

Did any of these solve it for you?

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top