Results 1 to 10 of 10

Thread: Numeric Code in Textbox?

  1. #1

    Thread Starter
    Addicted Member Smie's Avatar
    Join Date
    Jun 1999
    Location
    Columbus, OH
    Posts
    249

    Post

    Could someone please tell me how to make a textbox numeric only? This would help me greatly, thanks!

  2. #2
    Hyperactive Member Gimpster's Avatar
    Join Date
    Oct 1999
    Location
    Redmond, WA 98052
    Posts
    331

    Post

    It's pretty nasty, but you could just an if then statement in the Text1_KeyPress() event. Something like:

    Code:
    If KeyAscii <> vbKey0 or...KeyAscii <> vbKey9 Then
        x = Len(Text1.Text)
        x = x - 1
        Text1.Text = Mid(Text1.Text, 1, x)
    End If
    That way, if they type anything in that isn't a number, then it will delete the last typed character. It's a little sloppy, but it should work just fine.

    ------------------
    Ryan


    [This message has been edited by Gimpster (edited 01-03-2000).]

  3. #3
    Member
    Join Date
    Jan 1999
    Location
    Garden Grove, CA, Orange
    Posts
    55

    Post


    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If (KeyAscii >= 33 And KeyAscii <= 47) Or (KeyAscii >= 58 And KeyAscii <= 126) Then
    KeyAscii = 0
    End If
    End Sub


    HTH
    Joon

  4. #4
    Junior Member
    Join Date
    Jan 2000
    Location
    Bryan, Texas
    Posts
    27

    Post

    Here's the code that my VB book taught me... It seems simpler than the ones mentioned earlier.

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
         If KeyAscii > Asc("9") Then KeyAscii = 0
    End Sub
    Hope that helps.

    Mitchell Hayenga
    mitch@hayenga.com

  5. #5
    Member
    Join Date
    Dec 1999
    Posts
    37

    Post

    Another way....

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    
    If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
    
    End Sub
    Ruchi

  6. #6
    Member
    Join Date
    Jan 1999
    Location
    Garden Grove, CA, Orange
    Posts
    55

    Post

    mhayenga,

    I don't think it's a great deal, but it still takes some of special characters, which ascii number is less than character "9"

    If u want to allow only numbers in the textbox, u have to include the other condition to check for ascii numbers that are less than character "0".

    Ruchi,

    That works fine, except it doesn't take "BackSpace" key.

    Joon

    [This message has been edited by jpark (edited 01-03-2000).]

  7. #7
    Member
    Join Date
    Dec 1999
    Posts
    37

    Post

    Okay, if you want to use BackSpace key, then try this one ...

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> vbKeyBack Then
            KeyAscii = 0
        End If
    End Sub
    Ruchi

  8. #8

    Thread Starter
    Addicted Member Smie's Avatar
    Join Date
    Jun 1999
    Location
    Columbus, OH
    Posts
    249

    Post

    Thank you!

  9. #9
    Junior Member
    Join Date
    Jan 2000
    Location
    Bryan, Texas
    Posts
    27

    Post

    Ok, thanks for correcting me earlier... Also, you might want the If-Then statement to allow another ASCII value. The program might require the user to use a decimal point value. I don't know the value of a decimal point off the top of my head, but it should be in the VB help files under "Ascii character codes".

    Mitchell Hayenga
    mitch@hayenga.com

    [This message has been edited by mhayenga (edited 01-03-2000).]

  10. #10
    Junior Member
    Join Date
    Jan 1999
    Location
    Chennai, India
    Posts
    17

    Post

    Hi

    it's not diffcult to write code for allowing
    numbers in the text box. Most of our friends
    replied to u'r question.

    try some creative way ..

    all the best
    with love

    sanju

    ------------------

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