How to cycle through controls using TAB?

On one of my VB forms, I could use TAB to move between
controls according to TABINDEX. But when I come to the
highest TABINDEX, pressing TAB does not return me to the
lowest indexed control. Similar for Shift-TAB, it does not
bring me to the highest index control when I am at
TABINDEX 0. How can I change this?
Who is Participating?
AnswerTheManConnect With a Mentor Commented:
perhaps u have a tab index set to an unvisible control or to some other element on your form
I don't know which controls you have on your form, but there are several controls (such as PictureBoxes) that accept TabStops but they don't appear to have the focus.... I suggest you to set to False the "tabStop" property of all controls in you form that don't need the input focus.

Ditto on trillo's comments.  Also things like tabs.
If you are having trouble finding out which control it is.....
Put MsgBox ActiveControl.Name in the lost focus for what you believe to be the last control in your tab sequence
run the app...set the cursor in what you believe to be the final control in the tab sequence and press tab.
The message box will tell you the name of the control that now has focus.  Set it's Tabstop to false and continue until the cursor goes where you want it to.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

angelanyAuthor Commented:
I have UserControls on my form, and they also contain UserControls that accept input,
and VB controls such as buttons.

Usually, if I have all VB controls, when I press TAB, the focus will cycle through all
eligible controls (textbox, button, etc), from tabindex 0, 1, ... to max tabindex, and
one more TAB brings the focus back to tabindex 0. This is visually obvious on the
screen: textbox gets cursor, and button get dashed outline inside the border.

Now what happens to me is that, when I TAB through all controls and come to the
highest tabindex control (e.g. a button), the focus does not move on any more when
I press TAB. It got "stuck". Similar for Shift-TAB when the order is supposed to be

I know this should work because some other forms that have similar structure as
this form DO have the TAB key functional as expected. But I could not find out the
difference. Is there anything subtle about UserControls?
Just curious, but I am assuming there is no code on the control or form to suppress the tab key given focus on that last control?  Esp. since you are saying that the shift tab is not working either.  I had meant to post my answer as a comment, so go ahead and reject it.
If you would like, email me the form and I'll take a quick look at it.
you can set the tab  index manually ,

in the last control in the sequence , on the Lost_focus event you can use the GetFocus method of the first control in the sequence.

angelanyAuthor Commented:
Setting the tabindex manually does not help. The focuses get stuck
at highest and lowest indeces.

Now my problem is half solved. Since the last item on the form is a command button,
I could use the following code to set focus to the first field, which is a textbox:

Private Sub cmdClose_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyTab Then
    End If
End Sub

But while I am at the first field (the textbox), pressing Shift-Tab gives me a beep, and
the focus does not change. I can not use lastbutton_setfocus in the code of
firstfield_lostfocus since there are two directions where the focus can go
(TAB and Shift-Tab).

I can not post the form because it contains OCXs and will not show without
some services specific to our setup. I don't think there is anything in the code that suppresses TAB key.

I also noticed that the other forms that I claimed "working" all contain a tabstrip, which
has its tabindex at a higher level, while this form contains one and only UserControl.
What you need to do is check the Shift in the KeyDown event.  Your code should look something like this:

If KeyCode = vbKeyTab And (Shift And vbKeyShift) Then
End If

See if that works for you.


angelany, I think your UserControl is having problems. Check property CanGetFocus, ForwardFocus.
It is not unusual to have focus problems with UserControls.

E.g. put two textboxes on usercontrol. When using Tab on your form (which also has textboxes), everything looks OK. But when shift-Tabing, guess what will happen? Try it!

The workaround: replace Usercontrol with normal controls, or put everything in usercontrol, so it is the only control on the form.
Sorry, the code I posted earlier has an error.  It should read:

If KeyCode = vbKeyTab And (Shift And vbShiftMask) Then
    MsgBox "Shift Tab"
End If

Good Luck,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.