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

Only numbers in a textbox 5

Status
Not open for further replies.

Mollethewizard

IS-IT--Management
Nov 18, 2002
93
SE
I’ve got a slight problem. When a user enters a textbox the only permitted entry should be numbers or leaving the textbox empty. Entering letters should not be allowed.

Any code suggestions?

Furthermore I would like the opportunity to apologize to Geoff (xlbo) and all others for my impudent remark that I didn’t appreciate that someone told me how things should be done in reply to an answer from Geoff.

What I meant was that things can be done in several ways. Some are faster than others; some are shorter in code etc. All together the result would be the same.

I think that a lot of us that asks for code gets the base and then builds further and molds the code for our own purpose. I for one am not fishing for the fastest, most handsome code. Only a working code because I aren’t building macros with dozens of sheets with 256 columns and 65536 rows in each of them. If I were then the fastest code would be desirable.

I’ve taken time and have read a lot of contributions from Geoff and others, not mentioned by name, and I bend my head and bow in respect for all of your knowledge!

Greetings from Sweden

Christer
 
Christer,

To achieve this you can;

1. Set your control to a number datatype, which should prompt the user if text is entered.

2. On the controls change/update event, use

IF IsNumeric(YourControl) Then

Your Code

Else

MsgBox "Invalid Entry"
Me.YourControl.Value = ""
Exit Sub

End IF

Hope this helps.

Leigh Moore
LJM Analysis Ltd
 
Christer, here is another way you can consider. Put this code in the form's code page:
[blue]
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub
[/color]

Anything less that a zero (ascii 48) or greater than a nine (ascii 57) will be ignored. If you want to allow some special characters (e.g. a decimal point or the negative sign), you can code for that, too.

I always prefer to prevent the user from making a bad entry, rather that slapping his wrist (i.e., put up a message box) after a bad entry is made. My approach is, if the user can enter bad data, it is probably my fault, not his.
 
Zathras - I bend over and bow!

Your code is indeed very efficient though one doesn’t need any MsgBoxes that tells the user that he or she have entered the “wrong” signs!

But can’t we agree on that sometimes it feels good to slap “our” users wrists? Just kidding…..

Greetings from Sweden

Christer
 
What a cracking idea Zathras, have another twinkly thang!



Chris

Varium et mutabile semper Excel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top