Avatar of BradleyCleveland
 asked on

Cannot get cursor to move to designated textbox

I have code that runs on the keydown event of a textbox if the key stroke is the Enter key. The problem is that once my code is run I want the focus to come back to this same textbox and the textbox be empty and ready to receive keystrokes again. I can use the SetFocus command and even clear out the text box, but the mouse cursor is still sitting on a different textbox. Using the SelStart option doesn't work either. How do I get the mouse cursor to return to the textbox that is supposed to have the focus?
Microsoft Access

Avatar of undefined
Last Comment
Dale Fye

8/22/2022 - Mon
Dale Fye

Let me get this right.

You have a control that every time the user hits the enter key in this control, you want to do something, then you want to clear out the text that is already in that control, and then you want to put the cursor back in that control, is that correct?

Your problem is that you need to allow Access to process the various events that go along with hitting the Enter Key, namely the BeforeUpdate and AfterUpdate events.  But you also need to take into account the need to move the cursor into another control on your form.

If you record the last key pressed in a variable that is stored at the form level, in the KeyDown event, then in the AfterUpdate event of that control, you could check to see whether the last key pressed was the Enter key, and if so, you should be able to set the value of that control to NULL and then set the focus back to it.

I thought your idea might work but it didn't.  I also tried the on exit event, lost focus event and the enter and got focus of the control that the cursor lands in.

Any other ideas on this question before I close it out as unanswered?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Dale Fye

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

I had already done this previously EXCEPT for setting the Keycode to zero. Evidently that was what was needed.  Thanks so much.
Dale Fye

glad to help.