I am trying to program a text box to only accept numbers. I assume you would program the textchanged event of the text box to ignore the keystroke if it is something other than a number? Is that correct or is there a better way to do it?
Printable View
I am trying to program a text box to only accept numbers. I assume you would program the textchanged event of the text box to ignore the keystroke if it is something other than a number? Is that correct or is there a better way to do it?
VB Code:
Option Explicit Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = vbKeyBack Then Exit Sub If KeyAscii >= Asc("0") And _ KeyAscii <= Asc("9") Then Exit Sub KeyAscii = 0 End Sub
You have to be careful with the pasting too. Keypress event doesn't trap the user pasting text in the textbox.
Thanks,
I tried that and it's not working. Is the vbKeyBack either a function I'm not familiar with or needs to be declared somewhere? I apoligize ahead of time if that's a stupid question. I'm relatively new at this.
You can also click the NumberBox ActiveX control link in my signature. That will get you to code that you can compile to produce an OCX that limits input to numbers. It also allows you to specify maximim decimals and to decide if negative numbers are allowed.
Thanks Marty,
Since I am new to programming, how can I compile this code and add the control to my toolbox or project?
Just compile it like any other program, save the resulting OCX anyplace you want to (it's probably best to put it where your other OCXs are), go to Project|Components, browse for the new OCX, select it and then just add it to your form like the normal checkbox.
Please forgive my ignorance, but I'm still not sure how to do that. My only experience is with simple exe programs.
Try this
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) If (KeyAscii < 47 Or KeyAscii > 57) Then 'Disable keyboard lettering KeyAscii = 0 End If end sub
HTH
;) ;)
I tried your code, but that still allows me to type anything I want into the text box.
I have the same code in one of my app's and it works fine! ;)
are you change the procedure to whatever you have called your
txt box...
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) 'here the text1 should be whatever you have called your txt box
;)
Yes, I cut and pasted your code with the exception of the text box name. Here is my code:
VB Code:
Private Sub txtPNR_KeyPress(ByVal keyascii As Integer) If (keyascii < 47 Or keyascii > 57) Then keyascii = 0 End If End Sub
Still allows me to type anything into txtPNR. Numbers, letters, characters, etc.
I don't know what you've done but the following from your code is invalid.
Private Sub txtPNR_KeyPress(ByVal keyascii As Integer)
The standard textbox KeyPress event sub header would look like
Private Sub txtPNR_KeyPress(KeyAscii As Integer)
as Marty says take out the Byval keyword.....Where in my code
did you see byval...
;) ;)
visual basic code:
this is more than what you wanted but it is good code to add to your collection!VB Code:
Private Sub cmdSaveRecord_Click() If txtRegNo.Text = "" Then MsgBox "Registration number must be entered" txtRegNo.SetFocus Exit Sub ElseIf Not IsNumeric(Me.txtRegNo) Then MsgBox "You have entered a non-numeric value, please enter a number that is 5 digits in length", vbCritical, "input error" Me.txtRegNo.SetFocus Exit Sub ElseIf Len(Me.txtRegNo) < 5 Then MsgBox "You have entered a number less than 5 digits. Enter only a 5-digit number", vbCritical, "input error" Me.txtRegNo.SetFocus Exit Sub ElseIf txtFirstName.Text = "" Then MsgBox "First name must be entered" txtFirstName.SetFocus Exit Sub ElseIf txtLastName.Text = "" Then MsgBox "Last name must be entered" txtLastName.SetFocus Exit Sub End If 'When all data has been entered into text boxes and you press the command button (save record) it will be written and saved to a file for later viewing, if file isn't thier it will create it Open "C:\OffDataFile.txt" For Append As #1 Write #1, UCase$(txtFirstName.Text), UCase$(txtLastName.Text), UCase$(txtRegNo.Text), UCase$(cboMonth.Text), UCase$(cboDay.Text), UCase$(cboYear.Text), UCase$(cboClass.Text) txtFirstName.Text = "" 'data entered into these text boxes will be saved to a file txtLastName.Text = "" cboClass.Text = "" txtRegNo.Text = "" cboMonth.Text = "" cboDay.Text = "" cboYear.Text = "" Close #1 End Sub
hope it helps
sorry forgot to make it vb code.
You can always edit it to add the tags like I just did.
I am using VB.net and when I type the following:
private sub txtPNR_KeyPress (KeyAscii as Integer)
and I press enter, the text automatically changes to:
private sub txtPNR_KeyPress (ByVal KeyAscii as Integer)
??
I wish you had said so earlier. You posted in the Visual Basic forum. I'll move it for you now.