Link to home
Start Free TrialLog in
Avatar of BradleyCleveland
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?
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

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.
Avatar of BradleyCleveland
BradleyCleveland

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Dale Fye
Dale Fye
Flag of United States of America 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
I had already done this previously EXCEPT for setting the Keycode to zero. Evidently that was what was needed.  Thanks so much.
glad to help.