Results 1 to 11 of 11

Thread: whats wrong with this 1 line of code?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    Code:
    If Asc(Char) > 47 And Asc(Char) < 58 Then
    'bla bla ba
    end if
    an error keeps coming up to that line
    NXSupport - Your one-stop source for computer help

  2. #2
    Guest
    What error are you getting?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    Invalid procedure call or argument (run time error 5)
    NXSupport - Your one-stop source for computer help

  4. #4
    Guest
    i am assuming that this is what you want...
    in the keypress event put this...

    Select Case KeyAscii
    Case 8 'backspace key
    Case 13 'return key
    cmdQuery(1).SetFocus
    ' control to set focus on enter
    Case 48 To 57 ' chr(48) - chr(57)
    ' only numbers

    End Select

  5. #5
    Guest
    If only entering numbers what you want, then you can use the Like operator as well.
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        'Allow backspace, tab and spacebar to be pressed
        If KeyAscii = 8 Or KeyAscii = 9 Or KeyAscii = 32 Then Exit Sub
        If Not Chr(KeyAscii) Like "#" Then KeyAscii = 0
    End Sub

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    wait a sec, cna you correct my first line of code? becuase I need to make it so that you can only enter numbers and backspaces into a texbox
    NXSupport - Your one-stop source for computer help

  7. #7
    Guest
    Private Sub Text1_KeyPress(KeyAscii As Integer)

    Select Case KeyAscii
    Case 8 'backspace key
    Case 13 'return key
    'next control to get focus
    control.setfocus
    Case 48 To 57 ' chr(48) - chr(57)
    ' only numbers

    Case Else ' nothing else
    KeyAscii = 0

    End Select

    End Sub



    [Edited by larryn on 09-11-2000 at 06:31 PM]

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    can you make it something like:

    If Asc(Char) > 47 And Asc(Char) < 58 Then

    ? that includes Ascii and stuff
    NXSupport - Your one-stop source for computer help

  9. #9
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Exclamation Probably Char is empty

    If you have not put some value into char, then you will get the error you have mentioned.

    I suggest you check your logic because the only error I can see possible in your line at runtime is empty variable.

    The suggestions mentioned in other replies are a better approach to handling invalid characters on a Text_Change event. Better still in my view is to use the "masked edit control".

    Cheers
    Paul Lewis
    (Back from holiday and looking for something to do)

  10. #10
    Guest
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii = 9 Or KeyAscii = 8 Or KeyAscii = 32 Then Exit Sub
        If Not (KeyAscii > 47 And KeyAscii < 58) Then KeyAscii = 0
    End Sub

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

    <?>

    Code:
    Option Explicit
    'allow for backspace and numbers only
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    
    If KeyAscii = 8 Or _
        KeyAscii >= 48 And KeyAscii <= 57 Then
        KeyAscii = KeyAscii
    Else
        KeyAscii = 13
    End If
    
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

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