Numerical Data Only + Textbox

dynamix

Registered User.
Local time
Today, 23:11
Joined
Feb 9, 2005
Messages
38
Hi, in my project I have a ew fields such as Telephone number and I want to add validation to them to allow only numerical data input.

I'm currently using the following code but it doesn't work:

Private Sub Tel_KeyPres(KeyAscii As Integer)
'Only numeric data input
If KeyAscii = vbKeyTab Or KeyAscii = vbKeyBack Then Exit Sub
'Check for alpha characters
If Not IsNumeric(Chr(KeyAscii)) Then
MsgBox "Only numerical characters allowed"
KeyAscii = 0
End If

End Sub

Also, please tell me where in my code to put this.


Thanks so much!
 
It doesn't work because you're not getting rid of the bad character. I wouldn't use this event at all. I would use the field's BeforeUpdate event. That event will fire when all the characters have been typed and before the data is actually saved.

Code:
Private Sub Tel_BeforeUpdate(Cancel as Integer)
If Not IsNumeric(Me.Tel) Then 
    Msgbox "Only numerical characters allowed"
    Cancel = True
End Sub
I did not undo the field because I didn't want to force the user to retype all the characters.
 
Thanks a bunch Pat! Finally it works :D!
 

Users who are viewing this thread

Back
Top Bottom