Textbox and down arrow key problem

mps

New Member
Joined
Feb 7, 2011
Messages
42
I have four textboxes on my userform. The tabindex is set up so you can tab from textbox 1 to textbox 2 to textbox 3 to textbox 4 to commandbutton 1 to commandbutton 2 and then back to textbox 1.
This works fine.

However, if I press the down arrow in textbox 1 it jumps to textbox 3, pressing it again it jumps to commandbutton 2. Then if I press the up arrow it jumps to commandbutton 1, up arrow again jumps to textbox 4 and up arrow again jumps to textbox 2. It is frustrating me like anything. Why does not the up / down arrows follow the tab order that works fine????

If it helps I am using Excel 2010 on Windows 7 64bit. Thanks.

Martin
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I would really like help on this. If no-one can solve the issue of why the up / down arrow keys do this, can anyone tell me how to stop the user advancing to the next textbox etc. using the arrow keys and only allow them to do this by pressing enter or using the tab key?? Thanks.
 
Upvote 0
You could disable the arrows when initializing the userform, using something like
Code:
Application.OnKey "{Down}", ""
and re-enable them on exiting the form with
Code:
Application.OnKey "{Down}"
BUT I believe it is too risky.

Or you could use a variable to keep the name of the last visited control and if the next selected is not the one you expect move the focus on the next one. For example:
Code:
Dim Flow  'This on top of the module with the userform code
then
Code:
Private Sub Textbox2_Enter
If Flow<>"Textbox1" And Flow <> "" then Textbox3.SetFocus
Flow="Textbox2"
'any additional instruction
End Sub
Each of the textbox control should include a similar set of instruction, so that if when you "enter" a control you are not coming from his defined predecessor the next control will be entered.

Hope this helps.

Bye
 
Upvote 0
Trap the up and down arrow keys via each control's KeyDown event like this:
Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = vbKeyDown Or KeyCode = vbKeyUp Then KeyCode = 0
End Sub

Private Sub CommandButton1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = vbKeyDown Or KeyCode = vbKeyUp Then KeyCode = 0
End Sub
 
Upvote 0
Thanks John your code worked perfectly. I had previously tried Anthony's application.onkey solution but for some reason could not get it working - I wonder if it was because I was writing 'DOWN' in the code instead of 'Down'?

I am still curious as to why moving between controls using the arrow keys happened in way which was not consistent with the tab index - must be one of those strange Excel mysteries!!
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,849
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top