Hello,
How can I Force the user to insert only numbers in a textbox?
Or in a different way, How can I check under the KeyPress event if the user pressed 0 to 9? (I think that would be better)
Thanks
Printable View
Hello,
How can I Force the user to insert only numbers in a textbox?
Or in a different way, How can I check under the KeyPress event if the user pressed 0 to 9? (I think that would be better)
Thanks
Select the Text field in which you want to limit the user to numbers only. In it's properties, look for Dataformat, and click the "..." You should be able to choose General, Currancy, Number, Date, ect....
vb Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case vbKeyBack, vbKey0 To vbKey9 ' These keys are allowed to pass Case Else ' Everything else is blocked KeyAscii = 0 End Select End Sub
I think you could do it like this:
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("0") To Asc("9"), vbKeyBack
Case Else
KeyAscii = 0
End Select
End Sub
when I submited my reply, I found Logophobic had replied.
I think vbKey0 would be better than Asc("0"), ha ha.
I'm starting to remember slowly how to do stuff after a long time i didnt touch VB. I got this
Yours are better, I think I will go on the DataFormat suggestion. Thanks anywayCode:If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii <> 8 Then
MsgBox "Error"
KeyAscii = 0
End If
End If
Logophobic ?Quote:
Originally Posted by tanchuhan
Ive tried it, but it doesnt prevent me from typing letters...Quote:
Originally Posted by Ghostpk
Download and compile the NumberBox ActiveX control from my signature.
How aboutBy putting it in the Change event you cover not only typing but also pasting.Code:Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) Then
Msgbox "Only numbers allowed."
Text1.Text = vbNullString
End If