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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get a textbox to take numbers only?

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
I've done this before, but I forget. I also want the user to be able to press return, backspace and crsr keys.
But I only want numbers 0-9.
Am I missing something? %-)

Gary The door to life is never locked, but few have the knowledge to open it.
 
'Add 1 TextBox To Your Form.
'Insert the following code to your form:

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 47 Or KeyAscii > 57 Then KeyAscii = 0
End Sub

Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX
Source CodeBook for the programmer
 
Thank you so much. Ya know how you get brain-dead sometimes?
I was looking at the keydown event, and that did not cut it.

Here's my version (adding to yours)

Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 47 Or KeyAscii > 57) And KeyAscii <> 8 Then KeyAscii = 0
End Sub

This way they can use the backspace. :)
The door to life is never locked, but few have the knowledge to open it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top