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

Forcing TextBox to Accept Numbers Only 2

Status
Not open for further replies.

Igwiz

Technical User
Jun 3, 2003
88
CA
Hi,

I have designed a Userform and one of the textboxes allows the user to input a specific percentage to attach to a calculation. How can I force the textbox to only accept numbers?

Many thanks,

Ig
 
In the TextBox's Change Event, enter something like the following code.

Private Sub TextBox1_Change()
If Not IsNumeric(TextBox1) then
TextBox1.Value = ""
MsgBox "Please enter a number"
End if
End Sub

You can remove the MsgBox if you don't want the user to be warned.

Hope this helps!

If you can't be "The Best", be the best at what you can!!!

Never say Never!!!
Nothing is impossible!!!
 
Hello Igwiz

Try this...
it checks that the value entered is a number before the form is exited. there maybe a way to prevent them putting in anything other than numbers but i think that might be a longer way ( but i dont really know)
i use this method anyway and it works

cmdOk should be the name of your OK button (or equivalent if you have one)
txtPercent is name of the text box
and Percentage is the variable that stores the value entred.

Code:
Private Sub cmdOk_Click()
    If IsNumeric(txtPercent.Value) = False Then
        MsgBox "Percentage must be a valid number!", vbExclamation
    Else
        Percentage = txtPercent.Text
        'hide form or do calculations
    End If
End Sub
enjoy
 
Igwiz - if this has helped you (and it certainly looks like it), the TekTips way of saying thanks is to award a star. This is not just for the benefit of the contributor that helped but also for those users that search the archives as it helps them locate those answers to similar questions that have been marked as helpful or useful information. To award a star, simply click on the "Mark this post as a helpful / expert post" link at the bottom of the appropriate post

Rgds, Geoff
Quantum materiae materietur marmota monax si marmota monax materiam possit materiari?
Want the best answers to your questions ? faq222-2244
 
Thanks for your support Geoff!

I'm not a guru like you, but I try to help where I can, and I was (am?) starting to lose interest in helping people.

Thanks for noticing!!!

Bowers
 
Bowers - I only do this as I am a member of the TT round table and as such I try to help "moderate" the forums I use. Personally, I gave up giving answers for stars a while ago (although it is always nice to get them) - the reason I keep giving answers is partially 'cos I like helping people and partially 'cos things come up in here that never come up at work so it helps to increase my own knowledge by helping others....."everyone's a winner baby" ;-)

Rgds, Geoff
Quantum materiae materietur marmota monax si marmota monax materiam possit materiari?
Want the best answers to your questions ? faq222-2244
 
Geoff -

I'm not giving answers just to get stars. I'm here for the same reasons you mentioned above. I just find it frustrating that I don't get stars for some of my answers and other do. I guess I'm just a bit insecure, or maybe it's because I haven't been at this as long as you have.

Thanks for the pep talk.

If you can't be "The Best", be the best at what you can!!!

Never say Never!!!
Nothing is impossible!!!
 
Hey - everyone likes to have their effort acknowledged ! People who say thanks get my little reminder as they are not being unappreciative but maybe they don't know the site policies - depends on the number of posts they have made. People who don't even say thanks get red flagged straight away

Rgds, Geoff
Quantum materiae materietur marmota monax si marmota monax materiam possit materiari?
Want the best answers to your questions ? faq222-2244
 
I mean "their" way of course!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top