Need to detect right arrow key

chuckcoleman

Registered User.
Local time
Today, 15:07
Joined
Aug 20, 2010
Messages
380
I have a text box on a form that has an input mask. The input mask is: 09\-09" X "0\-09" X "0\-09;0;_ I have some vba code using the On Exit event from the text box where I need to detect if the user used the right arrow key AFTER entering the first digit, (in the second position where the 9 allows it), to move them to next digit input position. I can't figure out how to detect that the right arrow key was used. If the user uses the space bar AFTER entering the first digit, no problem; I just look for " ". I've tried Mid(TextBoxName, 2, 1) = vbKeyRight but it doesn't work.

Any ideas?

Chuck
 
You use the On KeyDown, KeyUp or KeyPress event.

If using the KeyPress you can substitute the space for

Code:
Private Sub ControlNameHere_KeyPress(KeyAscii As Integer)
  If KeyAscii = 46 Then ' if right arrow
     KeyAscii = 32
  End If
End Sub
 
Bob, it still doesn't work. I substituted ascii code 32, (a space) and added a MsgBox to prove that works. It does. But when I use ascii code 46, or what I thought was a right arrow, ascii code 39, neither works. I've used the KeyPress event of the text box. Any idea why it isn't working?
Chuck
 
Sorry, I didn't do thorough testing to make sure I had the right event and key code.


Put it in the KeyDown event like this:

Code:
    If KeyCode = 39 Then
        KeyCode = 32
    End If
 
Bob, still doesn't work:
Private Sub Table_Length_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyAscii = 39 Then
MsgBox ("Hey, I found a right arrow.")
KeyAscii = 57
End If
End Sub

It just doesn't recognize a right arrow. Note: the MsgBox is just for testing.

Chuck
 
It might not work when you have an Input Mask. I did forget to try it with one. See what happens if you temporarily remove the input mask.
 
Bob, I figured it out. Your suggested code changed in our communications and when I changed it to KeyDown I had KeyCode in the Private Sub....but KeyAscii as part of the If/Then statement. Making them both the same it now works. Thanks again for your help!

Chuck
 

Users who are viewing this thread

Back
Top Bottom