Home » excel » excel vba – VBA Tab Key putting actual Tab Value in Text Box instead of navigating to next control

excel vba – VBA Tab Key putting actual Tab Value in Text Box instead of navigating to next control

Posted by: admin March 9, 2020 Leave a comment

Questions:

I have a VBA form (in Excel if that matters) that contains text boxes.

On three occasions, I have found myself pressing the tab key to navigate to the next control, but instead an actual TAB is being put in the text box.

The form normally acts as it should, but it does concern me that this gremlin is showing up sporadically.

It has shown up on Office 2003 as well as Office 2007 on two different computers.

Has anyone else encountered this problem and, if so, how did you fix it?

How to&Answers:

I was able to reproduce the problem 100% of the time by launching Excel, immediately pulling up the form, and holding down the tab key.

If I change any code at all in the form and resave the workbook, the problem goes away. I’m going to chalk this up to a fluke compilation error within VBA.

Answer:

I created a form with three text boxes. I entered characters and tabbed onto the next for some time without being able to duplicate your problem.

The only way I can get an tab into the text box is by entering Ctrl+Tab. This might be embarrassing but backspace removes it so it is not a major issue. Is it possible that you are accidentally pressing Ctrl at the same time?

I find occasionally that if I mispress a key that the cursor jumps to somewhere else on the screen. I am not quite sure what I mean by “mispress”; it seems to be something to do with pressing two keys at once. This seems to be a feature of modern keyboards and how they detect which key has been pressed because I have encountered it on many different computers. The implication is that by mispressing a key, a control character (perhaps tab or ctrl+tab) is generated.

I also tried the following which worked and conceals the problem by removing the tab and moving on to the next control.

Private Sub TextBox1_Change()

  If InStr(1, TextBox1.Text, Chr(9)) <> 0 Then
    TextBox1.Text = Replace(TextBox1.Text, Chr(9), "")
    TextBox2.SetFocus
  End If

End Sub

Answer:

As a quick work-around, use this code in the control’s Exit event.

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    TextBox1.Text = VBA.Replace(TextBox1.Text, VBA.Chr(9), "")
End Sub

Answer:

Set the TabKeyBehavior property to False to get “Tab jumps to next field” behavior.

Answer:

This might solve the problem:

Public Sub MoveFocusToNextControl(xfrmFormName As UserForm, _
xctlCurrentControl As control)

Dim xctl As control
Dim lngTab As Long, lngNewTab As Long

On Error Resume Next

' Move focus to the next control in the tab order
lngTab = xctlCurrentControl.TabIndex + 1
    For Each xctl In xfrmFormName.Controls
        lngNewTab = xctl.TabIndex
        ' An error will occur if the control does not have a TabIndex property;
        ' skip over those controls.
        If Err.Number = 0 Then
            If lngNewTab = lngTab Then
                xctl.SetFocus
                Exit For
            End If
        Else
            Err.Clear
        End If
    Next xctl
Set xctl = Nothing
Err.Clear
End Sub