|
-
Mar 27th, 2007, 08:16 PM
#1
Thread Starter
Hyperactive Member
Insert only numbers in textbox
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
Last edited by Stiletto; Mar 27th, 2007 at 08:20 PM.
-
Mar 27th, 2007, 08:21 PM
#2
New Member
Re: Insert only numbers in textbox
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....
-
Mar 27th, 2007, 08:33 PM
#3
Re: Insert only numbers in textbox
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
-
Mar 27th, 2007, 08:34 PM
#4
Member
Re: Insert only numbers in textbox
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
-
Mar 27th, 2007, 08:36 PM
#5
Member
Re: Insert only numbers in textbox
when I submited my reply, I found Logophobic had replied.
I think vbKey0 would be better than Asc("0"), ha ha.
-
Mar 27th, 2007, 08:41 PM
#6
Thread Starter
Hyperactive Member
Re: Insert only numbers in textbox
I'm starting to remember slowly how to do stuff after a long time i didnt touch VB. I got this
Code:
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii <> 8 Then
MsgBox "Error"
KeyAscii = 0
End If
End If
Yours are better, I think I will go on the DataFormat suggestion. Thanks anyway
-
Mar 27th, 2007, 08:42 PM
#7
Thread Starter
Hyperactive Member
Re: Insert only numbers in textbox
 Originally Posted by tanchuhan
when I submited my reply, I found Logophobic had replied.
I think vbKey0 would be better than Asc("0"), ha ha.
Logophobic ?
-
Mar 27th, 2007, 08:48 PM
#8
Thread Starter
Hyperactive Member
Re: Insert only numbers in textbox
 Originally Posted by Ghostpk
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....
Ive tried it, but it doesnt prevent me from typing letters...
-
Mar 27th, 2007, 10:25 PM
#9
Re: Insert only numbers in textbox
Download and compile the NumberBox ActiveX control from my signature.
-
Apr 12th, 2007, 01:48 PM
#10
Re: Insert only numbers in textbox
How about
Code:
Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) Then
Msgbox "Only numbers allowed."
Text1.Text = vbNullString
End If
By putting it in the Change event you cover not only typing but also pasting.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|