Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

excel

vba

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?

like image 653
ray Avatar asked Apr 26 '10 14:04

ray


3 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.

like image 162
Tmdean Avatar answered Oct 21 '22 10:10

Tmdean


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
like image 34
Tony Dallimore Avatar answered Oct 21 '22 09:10

Tony Dallimore


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
like image 29
Peter Manchester Avatar answered Oct 21 '22 10:10

Peter Manchester