Results 1 to 8 of 8

Thread: Just Numbers

  1. #1

    Thread Starter
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158
    Any one know the easiest way to only allow numbers into a text box, I saw a reply to someone elses post a few weeks ago but didn't bother to note it, it was only about 1 line of code.

    Thanks in advance!!!

  2. #2
    Guest

    here is one way....

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    
        Select Case KeyAscii
            Case 8
                ' Backspace OK.
            Case 13
                ' Enter treated like Tab.
                Text2.SetFocus
            Case 48 To 57
                ' numbers only
            Case Else
                ' Reject all else.
                KeyAscii = 0
        End Select
    
    End Sub

  3. #3

    Thread Starter
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158
    I did one similar to that Larryn but using the vbconstants and it didn't work?

    But I'll give your code a go...Thanks!!!

  4. #4

    Thread Starter
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158
    I solved my one and tried yours Larryn. Thanks for your help!!!

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946
    Code:
    'allow only numeric data in a textbox
    
    Option Explicit
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
      If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
    End Sub


    [Edited by HeSaidJoe on 11-15-2000 at 03:45 PM]
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6
    Guest
    Actually, you should set the KeyAscii to 0 rather than 13.
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
    End Sub

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    duh..I knew that...it's those cheap drugs....ran out.LOL..
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  8. #8
    Hyperactive Member
    Join Date
    Oct 2000
    Posts
    400
    larryn, instead of calling text2.setfocus, you should use SendKeys "{TAB}". That way, you won't have to keep updating the code if you change text2's name or tab order.

Posting Permissions

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



Click Here to Expand Forum to Full Width