an error keeps coming up to that lineCode:If Asc(Char) > 47 And Asc(Char) < 58 Then
'bla bla ba
end if
Printable View
an error keeps coming up to that lineCode:If Asc(Char) > 47 And Asc(Char) < 58 Then
'bla bla ba
end if
What error are you getting?
Invalid procedure call or argument (run time error 5)
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
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
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
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]
can you make it something like:
If Asc(Char) > 47 And Asc(Char) < 58 Then
? that includes Ascii and stuff
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)
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
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