Hi,
My question is: How to tell the different between a character(A-Z)and a number(1-0) that has enter the textbox.
Example: I have text1 which allows ONLY numbers to be enter. If the user enters character, it'll show appropriate message.
Thanks
Printable View
Hi,
My question is: How to tell the different between a character(A-Z)and a number(1-0) that has enter the textbox.
Example: I have text1 which allows ONLY numbers to be enter. If the user enters character, it'll show appropriate message.
Thanks
Following function should be called from the keypress event in your text field.
Code:Public Function CheckKeyPress(nASCIIValueofKey As Integer) As Boolean
CheckKeyPress = True
If nASCIIValueofKey < Asc("0") Or nASCIIValueofKey > Asc("9") Or nASCIIValueofKey = 8 Then
'nothing, we will allow these characters
Else 'the user has pressed some key other than 0-9 or backspace
nASCIIValueofKey = 0 ' cancel the character
Beep
CheckKeyPress = False
End If
End Function
'place this in your keypress event for "textfield"
Dim wasKeygood As Boolean
wasKeygood = CheckKeyPress(KeyAscii)
If Not wasKeygood Then
KeyAscii = 0
textfield.SelStart = 0
textfield.SelLength = Len(textfield)
Exit Sub
End If