Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Only Numerical Characters?

Status
Not open for further replies.

mooktion

Vendor
Apr 13, 2005
6
GB
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!
 
Mind giving a bit more precise description than "doesn't work"?

Anyway, have a look here thread181-1031458, using the keydown, also doing a length check.

BTW - keys that forces change of focus (tab/enter) will usually not be received by this controls keypressed, but the keypressed event of the next control.

Roy-Vidar
 
What I mean by doesn't work is that nothing happens.. There is no error message or anything.. The validation isn't there because I can type text characters like A B C in there.
 
Try this. Define your field as text and define your input mask as:
!\(###")-"###\-####
This should keep the user from entering anything but numbers. Hope this helps.
 
The procedure is called [tt]Tel_KeyPres[/tt] in your code. Is that a typo?

It ought to be [tt]Tel_KeyPress[/tt], if the Tel control's OnKeyPress property refers to "[Event Procedure]".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top