|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Get rid of non-numeric character in a text box
Visual Basic 5.0
I had a text box named Subnet. When a user put in a character, a message pop up saying "only number is allowed." This works. The problem is, the non-numeric character that was typed into the text box does not go away. The statement KeyAscii = 0, suppose to work, but it does not. So then I comment that out and use the code SendKeys BACKSPACE, True, but it still does not work. The entire code below works with Access 97 and 2000 when I used KeyAscii = 0. How do I get it to work in VB5 ? ============================================ In Form Text box named "Subnet" Code:
Private Sub Subnet_KeyPress(Index As Integer, KeyAscii As Integer)
Public_Numeric_Only (KeyAscii)
End Sub
============================================
in Module1
Public Sub Public_Numeric_Only(KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii <> 9 Then 'Allow BACKSPACE, TAB and Period (decimal) through
'Only digits are valid characters.
If Chr(KeyAscii) < "0" Or Chr(KeyAscii) > "9" Then
'KeyAscii = 0 'Set character to null if out of range
SendKeys BACKSPACE, True
Beep
MsgBox "Only Numeric characters (0 to 9) is allowed", vbExclamation, "Error. Numeric characters Only."
End If
End If
End Sub
Last edited by linh : November 19th, 2003 at 02:32 PM. |
|
#2
|
||||
|
||||
|
My suggestion would be to use the Text_Changed Event of the textbox, and insert the code to check each character as they type it. That way, as soon as the user types in an alpha character in the numeric field, you could immediately display the error, and remove that single character.
__________________
Fisherman "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - A.Einstein |
|
#3
|
|||
|
|||
|
reply
Thank you Fisherman for your help. I got it figure out. All of the code below works if I used
Call Public_Numeric_Only (KeyAscii) If I left out the word Call, then it won 't work correctly. ========================================= Code:
Private Sub Subnet_KeyPress(Index As Integer, KeyAscii As Integer)
Public_Numeric_Only (KeyAscii)
End Sub
============================================
in Module1
Public Sub Public_Numeric_Only(KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii <> 9 Then 'Allow BACKSPACE, TAB and Period (decimal) through
'Only digits are valid characters.
If Chr(KeyAscii) < "0" Or Chr(KeyAscii) > "9" Then
KeyAscii = 0 'Set character to null if out of range
Beep
MsgBox "Only Numeric characters (0 to 9) is allowed", vbExclamation, "Error. Numeric characters Only."
End If
End If
End Sub
Last edited by linh : November 19th, 2003 at 03:46 PM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Get rid of non-numeric character in a text box |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|