by Muhammad Asghar Shah (1 Submission)
Category: Custom Controls/Forms/Menus
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Wed 15th August 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Simple Code to accept Only Numbers & decimal in a text box.
Private Sub Text1_KeyPress(KeyAscii As Integer)
'When the user presses a key, check to insure the KeyAscii value is:
' Between 48 - Ascii 0
' 57 - Ascii 9
' 8 - BackSpace
' 46 - Ascii .
If InStr(Text1.Text, ".") And KeyAscii = 46 Then
KeyAscii = 0
Exit Sub
End If
If Not ((KeyAscii > 48 And KeyAscii < 57) Or KeyAscii = 8 Or KeyAscii = 46) Then
KeyAscii = 0 '0 is Ascii Null
End If
End Sub