You can overcome the problem with a pair of subs. In this example of a working pair of scripts, I have a time card entry form.
TextBox1 is the hours in. TextBox2 is the minutes in. Because of idiots entering incorrect values, like spaces, or fat-fingering
numbers, I had to write a validation. But I wanted it to clear the invalid text and set the focus back into the textbox where
the invalid text had been entered. The form functioned with the same problem as noted in this thread. When the hours were entered
incorrectly, it would clear the text, display a message box, and it was
supposed to set the focus back on the hours box.
Unfortunately, the buffered tab would make it go to the minutes box.
So to correct this problem, I disabled TextBox2, which was the next tab index position. That prevented the focus from going there.
Then I cleared the text value from TextBox1, to eliminate the invalid text.
Next I set the focus back to TextBox1.
Lastly, I did a separate sub routine to reenable TextBox2 once the sub routine of clearing and setting focus on TextBox1 was complete.
The end result is what you are asking about. The two routines are below. And the order of operations matters. You have to clear the value
before setting the focus. Otherwise you end up in a loop; an escapable loop, but a loop nonetheless.
------------------
Private Sub TextBox1_Enter()
TextBox2.Enabled = True
End Sub
------------------
------------------
Works beautifully. Hope it helps, whoever has need of it.
Bookmarks