Consulting

Results 1 to 5 of 5

Thread: Solved: select case problem

  1. #1

    Solved: select case problem

    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!

  2. #2
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    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




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  3. #3
    VBAX Mentor anandbohra's Avatar
    Joined
    May 2007
    Location
    Mumbai
    Posts
    313
    Location
    The code should be in Key press event as stated by malik641

    code will be as


    [vba]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
    [/vba]
    Always Mark your Thread as Solved the moment u got acceptable reply (located under Thread tools)
    Practice this & save time of others in thinking for unsolved thread

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This version ignores improper entries.

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

  5. #5
    thanks all for the help and advice. worked like a charm.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •