Numeric keypress

bokaraton

Member
Joined
Nov 25, 2003
Messages
23
Does anyone know how to code this for vb.net?

Id like to prevent the users from keying in alpha characters.

Private Sub txtRec_KeyPress(KeyAscii As Integer)
If Chr$(KeyAscii) Like "[!0-9" & Chr$(vbKeyBack) & Chr$(46) & "]" Then
If KeyAscii = 13 Then
KeyAscii = 0 get rid of beep
If txtRec.Text > rs.RecordCount Or txtRec.Text < 1 Then
txtRec.Text = rs.AbsolutePosition
Else
rs.Move txtRec.Text - 1, adBookmarkFirst
Call FillForm
End If
Else
KeyAscii = 0 ignore the keystroke
Beep
End If
End If
End Sub
 
If you do not want the users to enter letters then you can check the if the key pressed was a number using the IsNumber shared method of the Char. Then you pass in the keychar passed into the event.
Code:
If Not Char.IsNumber(e.KeyChar) Then
   e.Handled = True
End If
 
Back
Top