Results 1 to 10 of 10

Thread: IsNumeric, brain lock...

  1. #1

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    This is driving me crazy, i learned vb 2 years ago, just now getting back into it, i can't remeber how i did this??
    I'm sure it's simple, but, if i have a text box, and want the user to only be able to enter numbers, how can i implement that. I can figure out how with using a message box. If the user tries to enter a letter in, it just wont let them. thanks

  2. #2
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    here you go:
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer) 
    
    If Not IsNumeric(Chr(KeyAscii)) Then 
    KeyAscii = 13 
    MsgBox "Numbers only"
    
    End Sub
    NXSupport - Your one-stop source for computer help

  3. #3

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496

    ..

    thanks for the reply, i figured out how to do that, but i dont want a msg box to pop up, just not to allow the user to enter letters. thanks

  4. #4
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    here:

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer) 
    
    If Not IsNumeric(Chr(KeyAscii)) Then 
    KeyAscii = 13 
    
    End Sub
    NXSupport - Your one-stop source for computer help

  5. #5
    Guest
    That will not allow backspace though dimava. Just a little addition...

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        
        If KeyAscii < 33 Then Exit Sub
        If Not IsNumeric(Chr(KeyAscii)) Then
             KeyAscii = 13
        End If
    
    End Sub
    By the way, is it:

    KeyAscii = 13
    or
    KeyAscii = 0
    ???

    [Edited by Matthew Gates on 11-03-2000 at 11:29 PM]

  6. #6
    Junior Member
    Join Date
    Mar 2000
    Posts
    28
    It's KeyAscii=0

  7. #7
    Guest
    You are right.

    0 = Nothing
    13 = Enter


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

  8. #8
    Lively Member
    Join Date
    Aug 2000
    Location
    Australia
    Posts
    82
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    'Another variation on the theme

    Select Case KeyAscii
    'Allow Backspace, 0 - 9 only
    Case 8, 48 To 57
    Case Else
    Beep
    KeyAscii = 0
    End Select

    Cheers
    Adrian.

  9. #9
    Hyperactive Member CyberSurfer's Avatar
    Join Date
    Aug 2000
    Location
    Old London Town
    Posts
    425
    I always did it this way...

    Code:
    Private Sub txtStuff_keypress(KeyAscii as Integer)
    
    If not isnumeric (Chr(KeyAscii)) and KeyAscii<>8 and KeyAscii<>46 then
    Beep
    KeyAscii = 0
    End Sub
    That allows for backspaces and decimal points...

    [Edited by CyberSurfer on 11-04-2000 at 10:04 AM]

  10. #10
    Guest
    The code I provided takes care of all keys like backspace, space, etc. etc. which you may need.

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        
        If KeyAscii < 33 Then Exit Sub 
        '^let's all these good/important keys pass
        If Not IsNumeric(Chr(KeyAscii)) Then 
         '^alphabet/!@#$%^&*()_+ = ignored
             KeyAscii = 0 
        'If the key pressed is not allowed, than don't allow
        'it to go through to the textbox 
        End If
    
    End Sub
    Short, Simple, and Sweet .

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