Control focus

  • Thread starter Thread starter J.C.
  • Start date Start date
J

J.C.

This is probably simple, but the answer eludes me...

Using the OnExit event, I am massaging a string acquired in
its associated Text Box control. In certain circumstances,
I wish to leave the VBA code and return to the form with
the focus still on the same control. Using
ControlName.SetFocus doesn't work - focus still proceeds to
the next control in the Tab Order. Can anyone point me in
the right direction?
 
You're using the wrong event. Use the BeforeUpdate event instead and when your condition is true and set Cancel=true

Private Sub Country_BeforeUpdate(Cancel As Integer)
If Country = "Canada" Then
'your condition is true so exit sub; you will stay at the same control
Cancel = True
Else
'do whatever
End If
End Sub
 
Vlad -

Yes, that worked, but now I can't store a corrected string
back to the control. Hmmm.

jc
-----Original Message-----
You're using the wrong event. Use the BeforeUpdate event
instead and when your condition is true and set Cancel=true
 
Try this generic example (use Cancel integer to cancel the movement of the
focus):

Private Sub txtBoxName_Exit(Cancel As Integer)
' your manipulative code goes here
If SomeCondition = True Then ' when true, keep focus on the textbox
Cancel = True
Exit Sub
End If
End Sub
 
Ken -

Yes, the Cancel did the trick. Thanks!

jc
-----Original Message-----
Try this generic example (use Cancel integer to cancel the movement of the
focus):

Private Sub txtBoxName_Exit(Cancel As Integer)
' your manipulative code goes here
If SomeCondition = True Then ' when true, keep focus on the textbox
Cancel = True
Exit Sub
End If
End Sub

--
Ken Snell
<MS ACCESS MVP>





.
 
Back
Top