Results 1 to 3 of 3

Thread: vbkeysubtract

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281
    Hi,

    Could someone please tell me what I'm doing wrong here? The only keys I want the user to be able to press are number 1 to 9 on the keyboard, the subtract key, and the back space key. For some reason I can't understand the subtract key isn't recognised.

    Private Sub Text5_KeyPress(KeyAscii As Integer)
    If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> vbKeyReturn And KeyAscii <> vbKeySubtract And KeyAscii <> 8 Then
    KeyAscii = 0
    End If
    End Sub


    Any help would be much appreciated!

  2. #2
    Lively Member
    Join Date
    Jun 1999
    Posts
    120
    KeyCode Constants (e.g. vbKeySubtract and vbKeyReturn)
    are applicable in either KeyDown or KeyUp events but not
    on a KeyPress event. You can use the ASCII code equivalent
    instead...Thus a minor change in your code will be to use the ASCII code for the minus sign (45) instead of vbKeySubtract...

    e.g.
    Code:
    Private Sub Text5_KeyPress(KeyAscii As Integer) 
       If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> 13 _
         And KeyAscii <> 45 And KeyAscii <> 8 Then 
             KeyAscii = 0 
       End If 
    End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    Thanks a lot.

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