-
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!
-
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
-