Link to home
Start Free TrialLog in
Avatar of Biggles1
Biggles1Flag for United States of America

asked on

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,

Biggles
ASKER CERTIFIED SOLUTION
Avatar of Anders Ebro (Microsoft MVP)
Anders Ebro (Microsoft MVP)
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Biggles1

ASKER

I actually had to use BOTH solutions!  Here's why:
txtSearch.SetFocus
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.

Biggles
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
txtSearch.selLength=0
Yep,
I agree with Anders Ebro,
As I stated, ...my 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.

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

Thanks again.