PDA

View Full Version : Solved: Need text box that only accepts numbers



clhare
05-13-2005, 06:08 AM
I am trying to set a text box up in a user form so that only numbers will be accepted. I have the following code, but it's not working. When I try to run the user form, I get a message that says "Run-time error '424': Object Required". What am I doing wrong?


Private Sub txtClientNbr_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 47 Or KeyAscii > 57 Then KeyAscii = 0
End Sub
Cheryl
:dunno

Jacob Hilderbrand
05-13-2005, 07:19 AM
Works fine for me. Just make sure the TextBox is named txtClientNbr.

Shellgrip
05-13-2005, 07:24 AM
Hey Cheryl, how about using IsNumeric


Private Sub txtClientNbr_Change()
If Not IsNumeric(txtClientNbr) Then txtClientNbr = ""
End Sub

MOS MASTER
05-13-2005, 10:24 AM
I am trying to set a text box up in a user form so that only numbers will be accepted. I have the following code, but it's not working. When I try to run the user form, I get a message that says "Run-time error '424': Object Required". What am I doing wrong?


Private Sub txtClientNbr_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 47 Or KeyAscii > 57 Then KeyAscii = 0
End Sub
Cheryl
:dunno
Hi Cheryl, :D

You're doing nothing wrong this code is fine!

Like Jake said you have to check if this eventhandler is attached to the correct object (textbox txtClientNbr)

You should also try to compile the project Menu/Error/Compile project
If any syntax errors or other typos are in your code the compiler will show you where they are!

To see if it is indeed the code that's wrong (which I find hard to believe) you can replace it with this for the same result:
Private Sub txtClientNbr_Change()
Dim sTemp As String
sTemp = Me.txtClientNbr.Text
If Not IsNumeric(Right(sTemp, 1)) And sTemp <> "" Then
Me.txtClientNbr.Text = Left(sTemp, Len(sTemp) - 1)
End If
End Sub


Enjoy! :whistle:

MOS MASTER
05-14-2005, 09:19 AM
Hi Cheryl, :D

Glad to see you have it working now!

Can you tell us what the source of your error was? :thumb

clhare
05-17-2005, 07:33 AM
It was my error. When I set up the user form (using an existing one), I removed some text boxes and didn't realize that I had originally set the focus to one of the text I now removed. So, the error was that the text box the focus was being set to no longer existed. Once I realized that and fixed the code, it worked.

Cheryl

MOS MASTER
05-17-2005, 09:59 AM
Yes a delete control will do that for yah...Thnx for the feedback! :whistle: