Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SetFocus to a control as part of event LostFocus

Status
Not open for further replies.

RayTango

Technical User
Jul 14, 2003
2
0
0
CA
This is a simple form. Choose Mr. Mrs. or Dr. from combo box
then enter name in TEXT box and finally gender M/F? also combo box.The code checks that Male only works for Mr. and Female for Mrs. Dr. can be either, like so...

Mr. SMITH Male
Mrs. Brown Female
Dr. Jones Female (or male as the case may be)

HERE is the question. The gender box is mandatory WHEN there is a name. if you try to leave it blank, on LOSTFOCUS then you get an error that says you must choose male or female. I want the focus to then return to the gender combobox and just keep looping this until either a M or F is specified, or the name field is cleared. I have tried this and get as far as the error message, but then focus always moves to the next control. Any suggestions?

Here is what I tried to use:

Code:
 If IsNull(Me!cmbSex.Value) Then
                    MsgBox "You must specify the gender of this guest.", vbOKOnly, "Incomplete entry"
                        Me!cmbSex.SetFocus
 
found this solution from another tip..........
on lost focus, set focus to another control and then back to the control you want

example

if textbox = "" or isnull(textbox) then
msgbox "message"
textbox1.setfocus a different control
textbox.setfocus back to itself
end if

it seems that an access control cannot setfocus directly to itself, but must be set to another control and then back to itself

shawn

I thank all of you. What you do for others is very special. What you are is special.

Shawn
 
Hi RayTango,

Is the Control's LostFocus event the best place for your code? Wouldn't it be better in the form BeforeUpdate? The focus may never get to your M/F box in which case your code will not run.

Hi traffas,

It's not so much that Access can't set the focus to the current control, it's more that it has no effect.

The LostFocus event occurs before the focus is actually moved; there is something else (normally another control) stacked up waiting to get the focus. Setting the focus explicitly to another control artificially clears that stack after which you can set it back to the current control and it won't get reset. The thing to watch out for is that there are no events triggered (GotFocus, LostFocus) on the other control.

Enjoy,
Tony
 
Thanks Traffas for the Solution, and also thanks Tony for considering the BeforeUpdate. I have also considered the points that you broguht up and may change the events to address this, but ofr now the workaround suggestion fr0m Tarrafs is what I was actually after.

*Man creates because God created him in his own likeness. Honor the gift he gave you.

Ciao
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top