How to Limit Text box input to Numbers Only

0 comments

Posted on 15th April 2010 by Kenneth in Visual Basic 6

, , , , , ,

‘Declaration
Const DecNum = “1234567890.”
Const IntNum = “1234567890″

‘Call this function for Decimal Numbers

Function ConvertDec(KeyAscii) As Integer ‘Decimal
If KeyAscii > 26 Then
If (InStr(DecNum, Chr(KeyAscii)) = 0) Then
Numb = 0
Exit Function
End If
End If
ConvertDec = KeyAscii
End Function

‘Call this function for Integer Numbers Only
Function ConvertInt(KeyAscii) As Integer ‘Integer
If KeyAscii > 26 Then
If (InStr(IntNum, Chr(KeyAscii)) = 0) Then
Numb = 0
Exit Function
End If
End If
ConvertInt = KeyAscii
End Function

‘Sample Code in text box
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = ConvertDec(KeyAscii) ‘Decimal Numbers only
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
KeyAscii = ConvertInt(KeyAscii) ‘Integer Numbers only
End Sub

‘Sample Code in text box
Private Sub Combo1_KeyPress(KeyAscii As Integer)
KeyAscii = ConvertDec(KeyAscii) ‘Decimal Numbers only
End Sub
Private Sub Combo2_KeyPress(KeyAscii As Integer)
KeyAscii = ConvertInt(KeyAscii) ‘Integer Numbers only
End Sub