Moving Insertion Point (Cursor) using VBA

In my MS Access application I have a Text Box with some characters inside a form.  The Text Box is called txtSearch.

I need to use VBA to move the Insertion Point (Cursor) beyond the last character in the txtSearch text box, so that the next keyboard input will add a character to the end of the text.

I use txtSearch.SetFocus, to send the Curson to txtSearch, then SendKeys {Right} but that sends the Cursor to to the next FIELD!

Please help,

Who is Participating?
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.

Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
You could use this:
   txtSearch.SelStart = Len(txtSearch.Text)

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
Jeffrey CoachmanMIS LiasonCommented:
What is posted above is the more accepted approach.

For fun, the SendKeys would be:
    SendKeys "{F2}"


Biggles1Author Commented:
I actually had to use BOTH solutions!  Here's why:
txtSearch.SelStart = Len(txtSearch.Text)  

does the job BUT the whole text in the Text Box is highlighted, so the next character typed replaces the whole text.

However adding  SendKeys "{F2}"

UN-Highlights the text and places the insertion point at the far right of the text so the next character typed is simply added to the Text Box.

Thanks Guys! Excellent Solutions.

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
That is odd. On mine it works as expected, the whole text is selected after the txtSearch.SetFocus, but NOT after the
txtSearch.SelStart = Len(txtSearch.Text)

In general its a good idea to avoid using SendKeys. Primarily because the user could have hotkeys mapped to e.g. F2 which might trigger odd and unexpected behaviour
Instead try to add this, either before or after setting the SelStart
Jeffrey CoachmanMIS LiasonCommented:
I agree with Anders Ebro,
As I stated, Sendkeys was just an FYI.

Another concern with Sendkeys is that every so often rumors circulate that MS will deprecate it.

The other thing is that some developers will start to depend on Sendkeys to do things,  (navigating menus, ...etc),  that can be done using more efficient means.

Sendkeys will not work as expected in all situations:
Some times the "Wait" argument is needed
In other rare instances, ...the sendkeys command must go before or after the obvious place due to timing, system, or modal dialog box issues.

Biggles1Author Commented:
I tried txtSearch.selLength=0 .  It stops the OnChange event from executing!  Back to using F2.  But I do share your concern.

Thanks again.
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 Access

From novice to tech pro — start learning today.