I would like to allow only string press from keyboad and spacebar are acceptable. How to do that?
Printable View
I would like to allow only string press from keyboad and spacebar are acceptable. How to do that?
If I understood you then you wish to disallow numeric input.
If so then try this quicky:
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 0
End If
End Sub
:wave:Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If InStr(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890", UCase(Chr(KeyAscii)), vbTextCompare) > 0 Then
Else
KeyAscii = 0
End If
End Sub
EDIT : remove or add anything you need to allow/disallow in the string
Using InStr will also prevent you from using say BackSpace which is most likely needed for editing.
Here is another variation of what I posted previously:
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not (KeyAscii = 32 Or KeyAscii = 8) Then 'allow using spacebar and backspace keys
If Asc(LCase(Chr(KeyAscii))) < 97 Or Asc(LCase(Chr(KeyAscii))) > 122 Then
'disallow anything but alpha chars (Aa-Zz)
KeyAscii = 0
End If
End If
End Sub
zeezee,
Can you please explain how that works?
It looks like you are taking the keyboard character entered, temporarily making it upper case and then comparing it to the string "ABCDE........ 1234567890" and if found do nothing but if not found then set KeyAscii to 0 but I don't understand it. Me not too bright today.
In other words, you do not want to allow numeric input, correct?Quote:
Originally Posted by matrik02
What the difference with your previous code #2 ?
Yes.Quote:
Originally Posted by Hack
@Rhino
Yeah You are right, Just felt lazy to test it tharoughly :blush:
Sorry for the mistake
@jmsrickland
Yeah you are right, just chcecking whether its in the big string (ABCD ...)
keyascii is ascii of the keypressed, so need to convert to character -> Chr
Then if its in the big string, it would return the position, a number > 1. So our true part is inverse of that, so if its not in the string, make the keyascii = 0 to prevent it bieng set in the text box.
Got it?
:wave:
If you can tell to whom is your question addressed please?Quote:
Originally Posted by matrik02
Thank you RhinoBull and the other, it work now