VBA Code - How to tab from one TextBox to another so that after tabing the cursor is at the end of all the text in the next TextBox

FaheemAhmadGul
FaheemAhmadGul used Ask the Experts™
on
On a VBA user Form I have two Text Boxes (named txtFirstName and txtLastName) and I frequently need to tab from one to another.

Currently when I tab from textbox to another all the text in the next Text Box gets selected. I would like to be able to tab to the next text box such that after pressing the tab key the cursor is at the of all the text in next TextBox.

I would appreciate help with how I set the properties of these Text Boxes so I achieve the above objective - that is after pressing the tab key when I get into the next textbox the cursor is at the end of all the text in the next text box.
If there is any VBA code which could enable such behaviour I would appreciate help with that also. Thanks.
I include an image to show what I am getting now and how I would like it to be.

Sample Image
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jim Dettman (EE MVE)President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012

Commented:
You would use the SelStart and SelLength properties to position the cursor.

In the OnEnter or GotFocus event, do:

   Me.txtLastName.SelStart = Len(Me.txtLastName)
   Me.txtLastName.SelLength = 0

Jim.

Author

Commented:
Thank  you for your comment. The TextBox (in my case txtFirstName or txtLastName) does not seem to have GotFocus or OnEnter events.
I am using these text boxes on an Excel VBA form.
Check your text box properties for EnterFieldBehavior. If that shows as an option, set it to "1-fmEnterFieldBehaviorRecallSelection"

Paul
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Author

Commented:
The TextBox does not have EnterFieldBehavior either
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
Open your UserForm in design mode and select the TextBox txtFirstName and press F4 to open it's Properties Window and you will find the EnterFieldBehavior property as shown in the image below. Expand the dropdown next to it and change it to 1-fmEnterFieldBehaviorRecallSelection.

TextBox.jpg
Roy CoxGroup Finance Manager

Commented:
You can simply use the Enter event

Private Sub txtLastName_Enter()
    With Me.txtLastName
        .SelStart = Len(.Value)
        .SelLength = 0
    End With
End Sub

Open in new window

Author

Commented:
Thank you very much all for your very helpful comments which have completely answered my question. All the comments helped my understanding of VBA. Comment from Flyster which came last night was a complete solution though I am sorry I could not understand it last night how to implement that, but Neeraj’s screen shot has helped me understand what I needed to do.
Thank you very much once again for all your help. Greatly appreciated.
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
You're welcome Faheem! Glad we could help.
Jim Dettman (EE MVE)President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012

Commented:
Sorry for the delay, but I have a got focus event here:

Region-Capture.jpg
Jim.
You're welcome Faheem! Like Subodh, I did have a screenshot, I just forgot to post it!! Glad you got it working.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial