PDA

View Full Version : Solved: Using arrow keys to navigate through userform fields



Cosmo
07-31-2008, 12:25 PM
I have a userform which has several sets of text fields (on several tabbed multiPages) arranged in a grid. I just became aware of a user who uses the arrow keys to navigate through the form. Apparently, the left and right keys don't navigate, but the up and down keys do.

I was having a problem with some of the fields which didn't always work properly with the up/down arrows, but I believe I have tracked that down to the fact that there was a 1 pixel overlap on those fields.

Now that I have the up and down navigation working, other than a custom keydown function, is there any way to have it navigate between the fields with the right and left arrows?

mdmackillop
07-31-2008, 01:01 PM
I tried the following but the right and left arrows don't trigger the subs when the userform is displayed.:dunno . It works on the speadsheet though; switch the + sign in the subs to get the arrows working the "wrong" way.

You can always use Tab and Shift+Tab


Private Sub UserForm_Initialize()
Application.OnKey "{RIGHT}", "MoveRight"
Application.OnKey "{LEFT}", "MoveLeft"
End Sub

'in standard module
Sub MoveRight()
Application.SendKeys "{Tab}"
End Sub

Sub MoveLeft()
Application.SendKeys "+{Tab}"
End Sub

Cosmo
07-31-2008, 01:16 PM
Thanks. I'll advise the users to use the tab/shift tab.

If they wish to use the keys, the following seems to workPrivate Sub txtVials_03_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
' 37 = left arrow, 38 = up arrow, 39 = right arrow 40 = down arrow
If KeyCode = 37 Then
'Right key pressed
Me.Controls("txtVials_02").SetFocus
ElseIf KeyCode = 39 Then
'Right key pressed
Me.Controls("txtVials_04").SetFocus
End If
End Sub

Bob Phillips
07-31-2008, 02:47 PM
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 39 Then KeyCode = 9
End Sub

Cosmo
07-31-2008, 03:01 PM
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 39 Then KeyCode = 9
End Sub

Ooooh, that's perfect!

...any way to set key KeyCode 39 to a 'shift-tab'?


EDIT - Nevermind, I should be able to set the 'Shift' parameter

Cosmo
07-31-2008, 03:06 PM
Nevermind, I should be able to set the 'Shift' parameter
Hmm,. that doesn't seem to be workingPrivate Sub txtVials_04_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 39 Then KeyCode = 9
elseIf KeyCode = 37 Then
KeyCode = 9
Shift = 1
End If
End Sub

:(

Bob Phillips
07-31-2008, 03:21 PM
I believe Shift is read-only



Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 39 Then
KeyCode = 9
ElseIf KeyCode = 37 Then
Application.SendKeys "+{TAB}"
End If
End Sub

Cosmo
08-01-2008, 05:11 AM
I believe Shift is read-only



Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 39 Then
KeyCode = 9
ElseIf KeyCode = 37 Then
Application.SendKeys "+{TAB}"
End If
End Sub

Excellent, thanks.

I really appreciate all of your input on this forum. I wish I had your brain.

Bob Phillips
08-01-2008, 05:24 AM
It's a shame that the little extension you tried didn't work isn't it! Much neater than using nasty Sendkeys.

Cosmo
08-01-2008, 05:50 AM
It's a shame that the little extension you tried didn't work isn't it! Much neater than using nasty Sendkeys.Yeah, it would have been preferrable, but as long as it works...

Now I just have to find out if the client wants me to remap the arrow keys to tabs for the MultiPage items on the userforms (since they, by default, use the arrow keys to scroll through the individual tabbed pages) to keep the interface consistent. Otherwise, when you scrolled through the last text field via the arrow key, it would start changing through the pages.