Change focus to control

J

John Barnes

Have a subform which contains two dates. Have code
written to check for WorkEndDate >= WorkStartDate. Code
is assigned to OnLostFocus. If ... Then statement
executes code properly and displays a message box alerting
user that the work end date must be greater than or equal
to work start date.

My problem is that I can't figure out how to return focus
to the WorkEndDate on the subform to force the user to
change their entry. Have tried macro with GoToControl, as
well as WorkEndDate.SetFocus. Neither work, the focus
moves to the next control in the tab order.

If I put a macro button on the subform and run GoToControl
from macro, then focus sets properly.

I need to find out how to set focus back to WorkEndDate
from within code (as part of if then statement.)

Thanks!
 
K

Ken Snell [MVP]

Use the Exit event, not the LostFocus event. The Exit event allows you to
modify the focus as part of the event, the LostFocus event is too late to
change where the focus is going.
 
J

John Barnes

Ken,

Thanks for your reply. Changed code to OnExit, but still
have exact same problem. I can access the control fine
through macro (using SetValue to Null), but focus still
moves to next control even though I tried both SetFocus in
macro and .SetFocus in code.

Any idea what I'm missing?

Thanks.
 
K

Ken Snell [MVP]

You need to Cancel the Exit event and then set the focus where you want it
to go.

Private Sub ControlName_Exit(Cancel As Integer)
If MyTestCondition = True Then
Cancel = True
Me.DesiredControlName.SetFocus
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Subform field focus 3
An alternative to SendKeys --"KeyStrokes: {F9}" Wait: "Yes" 8
Close on Lost Focus 2
Subform control OnClick firing 1
Can't Move Focus Problem 1
Set Focus 2
Help on Help 3
GoControl Problem 3

Top