PDA

View Full Version : Solved: select case problem



wolf.stalker
01-27-2008, 09:05 PM
hi all:

first time to site, and hopefully not the last. i have been working on a project and could use some help.

here is the jist of it. my source data is read in through a text box called txtUnlGross. what i am trying to do is to prevent any key other than 0-9 being entered.

my first attempt at this resulted in that it would function, but not alow any number with the 9 in it. for example, you could key in something like 10000 but you would get an error msg from 89 or 90 or 19 etc.

Anyone have any idea why i am getting this error?

Private Sub txtUnlGross_Change()

Select Case txtUnlGross.Value
Case 0 To 9
'do nothing
Case ""
'do nothing
Case Else
MsgBox ("This must be a number less than 15,000.")
txtUnlGross = ""
txtUnlGross.SetFocus
Exit Sub
End Select
txtUnlNet.Enabled = True
End Sub

thanks!

malik641
01-27-2008, 09:37 PM
Hi,

Welcome to the Forum.

You need to use the _KeyPress() event of the text box. Then check the Ascii character code entered and use from 0 to 9. (Hint: Use Asc(string) function)

That should do exactly what you're looking for.

EDIT: Oh yeah, and if they try to enter something OTHER than a number, just make KeyAscii = 0

anandbohra
01-27-2008, 10:15 PM
The code should be in Key press event as stated by malik641

code will be as


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 ' for number 0-9 (in excel to find out code use this formula =code(9)
'do nothing
Case Else
MsgBox "only numericals are allowed"
KeyAscii = 0
End Select

End Sub

mikerickson
01-28-2008, 01:26 AM
This version ignores improper entries.

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
KeyAscii = KeyAscii * (47 < KeyAscii) * (KeyAscii < 58)
End Sub

wolf.stalker
01-28-2008, 02:49 AM
thanks all for the help and advice. worked like a charm. :clap2: