VBA TAB SEQUENCE between TextBoxes placed in Document

I have in a Word Document 6 TextBoxes placed directly on the page.
I need to jump from one to another in sequence using TAB key.
How do I do that?

With a VBA UserForm would be easy to set the TabIndex.
Assuming that my TextBoxes are named TB0…TB5, no necessarily placed in such order on the page then I would need for instance if I write text in one of the TextBoxes, I need to jump to the next one in the order from page when I press TAB.
For example: TB0-->TB2-->TB3-->TB4-->TB5-->TB1-->TB0
An example would be the attached file.
The only idea that crossed to my mind is to read the keystroke (how?) at any event as TextBox Change and to check the last ASCII character.
Do you have any other ideas? Or if the above one is good enough, then how do I implement it in VBA Word?
VBA-TAB-JUMP-SEQUENCE.doc
LVL 21
viki2000Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dlmilleCommented:
0
dlmilleCommented:
There might be another alternative using the keydown event and trapping the keycode 9 (for tab), and using the API to get the scancode for SHIFT (for SHIFT-TAB).  Before I proceed, exactly what type of textboxes did you create on the word document?  ActiveX or Forms TextBoxes?  Also, what version of Word are you using?

Dave
0
viki2000Author Commented:
Which part from that page?
0
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

dlmilleCommented:
? unsure of your question - can you rephrase that?

Dave
0
viki2000Author Commented:
In mean time I figured out by myself, but I do not mind another solution better, simpler.
The code that I found is:

Private Sub TBChange(TBx, TBy As TextBox)
If TBx.Value = "" Then TBx.Value = " "

If Asc(Right(TBx.Value, 1)) = 9 Then
    TBx.Value = Mid(TB0.Value, 1, Len(TBx.Value) - 1)
    TBy.Activate
End If
End Sub

Private Sub TB0_Change()
Call TBChange(TB0, TB2)
End Sub

Private Sub TB1_Change()
Call TBChange(TB1, TB0)
End Sub

Private Sub TB2_Change()
Call TBChange(TB2, TB3)
End Sub

Private Sub TB3_Change()
Call TBChange(TB3, TB4)
End Sub

Private Sub TB4_Change()
Call TBChange(TB4, TB5)
End Sub

Private Sub TB5_Change()
Call TBChange(TB5, TB1)
End Sub
0
viki2000Author Commented:
You gave me a link, but I do not understand what code do I need to take/use from that webpage.
0
dlmilleCommented:
I was working this:

 
Private Sub TB0_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 9 Then TB2.Activate
    
End Sub


Private Sub TB2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 9 Then TB3.Activate
    
End Sub

Private Sub TB3_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 9 Then TB4.Activate
    
End Sub

Private Sub TB4_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 9 Then TB5.Activate
    
End Sub

Private Sub TB5_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 9 Then TB0.Activate
    
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dlmilleCommented:
The link was a bit overboard.  I think either your solution or mine is a bit simpler.

Dave
0
viki2000Author Commented:
I like yours and you spent your time.
Thank you for your time and help.
0
dlmilleCommented:
Well - mine will work if you use ActiveX controls.  Your textboxes weren't so the events weren't firing quite right on the keydown events.  I was in the process of deleting those and adding back with ActiveX controls - so then it should work properly.

See attached & thanks for the points!

PS - do you want it to work with SHIFT-TAB as well?

Try the attached!

Dave
VBA-TAB-JUMP-SEQUENCE.doc
0
viki2000Author Commented:
Yours have a small "bug". After you press TAB, the text box still have that TAB in it and has to be removed as I did it.
Additionally if you try to write some text and then to push TAB, for some reason in some textboxes the jump is not done.
Di you noticed the above "bugs" ?

So, has to be debugged, but that#s OK - the idea behind is good enough.
0
viki2000Author Commented:
OK. I red it later.
Thanks.
0
dlmilleCommented:
There are no tabs left in the textboxes after pressing TAB.  At least not when I run it from here (using Word 2010 right now).

Ahhh- I think you are NOT testing the version I uploaded - just the code I posted.  That code should go with correct TextBox ActiveX controls.

This version (please download and test it!) supports both tab and shift-tab. No bugs.

Cheers,

Dave
VBA-TAB-JUMP-SEQUENCE.doc
0
viki2000Author Commented:
You know, the first file had ActiveX controls in it as TextBoxes, so was not that the problem.
I think I found the “bug” to your code: the code in the above file that you posted is working well, but your file compared with my initial file in DesignMode the TextBoxes are formatted different:
- I set Properties, Format Control, Layout = “In Front of text” and you made it “In Line with Text”
- If in your Word file you change these properties then you have that behavior: sometimes TAB works, sometimes not.
Just as aside discussion.
0
dlmilleCommented:
Gotcha.  First time for textboxes in Word for me, lol.  Thanks for the feedback, and glad my contributions work for you.

I find if I tab real fast, or hold down the tab key, I get unusual results.  I was wondering if you get the same unusual results with your setup.

Happy Holidays!

dave
0
viki2000Author Commented:
Thank you for help and discussion.
I am Christian.
Merry Christmas Dave!

0
dlmilleCommented:
Merry Christmas.
0
viki2000Author Commented:
I write the bellow comments for future reference only.
Because I need/want the TextBoxes to be with Properties in Layout as “In Front of Text” I had to choose my initial code.
Now, no matter if I choose my code or the other suggested code I have an error when I protect the document.
In my original document, more complex than the example above, I have also text in pages and I want to protect the document.
After protection (Editing Restrictions = Comments) the Method used “.Activate” does not work anymore.
The solution that I found is to refer to the TextBoxes as Shapes in Document and use “.Select”
In such case the Shape number has to be identified according with the TextBox Name, so I wrote and additional function.
The code is bellow.

Private Sub TBChange(TBx, TBy As TextBox)
If TBx.Value = "" Then TBx.Value = " "
If Asc(Right(TBx.Value, 1)) = 9 Then
    TBx.Value = Mid(TBx.Value, 1, Len(TBx.Value) - 1)
    ActiveDocument.Shapes(IDShape(TBy)).OLEFormat.Object.Select
End If
End Sub

Function IDShape(TBy As TextBox) As Integer
Dim o1 As Object
Dim i As Integer
Set doc = ActiveDocument
For i = 1 To doc.Shapes.Count
Set o1 = Shapes(i).OLEFormat.Object
If o1.Name = TBy.Name Then
    IDShape = i
End If
Next i
End Function

Private Sub TB0_Change()
Call TBChange(TB0, TB2)
End Sub

Private Sub TB1_Change()
Call TBChange(TB1, TB0)
End Sub

Private Sub TB2_Change()
Call TBChange(TB2, TB3)
End Sub

Private Sub TB3_Change()
Call TBChange(TB3, TB4)
End Sub

Private Sub TB4_Change()
Call TBChange(TB4, TB5)
End Sub

Private Sub TB5_Change()
Call TBChange(TB5, TB1)
End Sub
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.