Control focus

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?
 
V

Vlad C

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
 
T

TTGroup

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
 
K

Ken Snell

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
 
J

J.C.

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>





.
 

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

Top