Limit Text Box Entry to Numbers Only.

0 comments

Posted on 15th April 2010 by Admin

Limit text box or combo box to numbers only (Integer or Decimal).

'Thiscode will limit the entry to numeric values only through the keypress and keyascii technique
Const DecOnly = "1234567890."
Const IntOnly = "1234567890"
'This function accept decimal numbers
Function ToDec(KeyAscii) As Integer 'Decimal
If KeyAscii > 26 Then
If (InStr(DecOnly, Chr(KeyAscii)) = 0) Then 'And KeyAscii <> 8 And KeyAscii = vbKeyZ And KeyAscii = vbKeyS) Then
Numb = 0
Exit Function
End If
End If
ToDec= KeyAscii
End Function
'This function accept Integer numbers
Function ToInt(KeyAscii) As Integer 'Decimal
If KeyAscii > 26 Then
If (InStr(IntOnly, Chr(KeyAscii)) = 0) Then 'And KeyAscii <> 8 And KeyAscii = vbKeyZ And KeyAscii = vbKeyS) Then
Numb = 0
Exit Function
End If
End If
ToInt= KeyAscii
End Function
'Now, call this function in keypress event. Sample Code below.
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = ToDec(KeyAscii) ' Accept numbers with decimal
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
KeyAscii = ToInt(KeyAscii) ' Accept integer numbers only
End Sub

No comments yet.

Leave a comment