SetFocus set's focus to next control on form.

dkinley

Access Hack by Choice
Local time
Today, 12:21
Joined
Jul 29, 2008
Messages
2,016
I've been stymied over this one awhile ... can't seem to figure out the hangup and wondered if anyone had seen or heard.

First off, Access 2007.

Code on a command button the user click's to exit the form ....

If Len(Nz(Me!txtControl, "")) = 0 Then
MsgBox "You must enter something.", , "Control Error"
Me.txtControl.SetFocus
Else
... do the exiting stuff.

Peculiarities:
- It sets the focus on the NEXT item in the tab order (doesn't matter control placement on screen.)
- Works on 30 other forms but not with two.
- No code difference between a working form and an discrepant form (although the discrepant forms are unbound forms, but I have unbound forms it works with no problems.)
- No form properties difference (stared and compared)

Ideas? Or something else that is cracked out with '07?

-dK
 
You cannot successfully set the focus to the control that is executing *in* the control that is executing. To hold the focus in the current control you would set Cancel = True in one of the cancellable events like the Exit event.
 
That's what it sounded like to me, too, Allan, like trying to set focus on a textbox that hadn't really lost focus, but the OP claims the code is in the Click event of a button, which cannot be Canceled. I tried to reproduce the behavior and couldn't. If TextBoxA has focus, and you click a button whose code is Me.TextBoxA.SetFocus, focus can return to TextBoxA, because it lost the focus when you clicked the button!

Assuming the situation is as reported, my only other guess would be corruption. Maybe delete the button and recreate it?
 
Thanks for the replies!

Correct, the text box loses focus when the user clicks the "OK" button and the code sends the focus back to the button.

I haven't thought about deleting the button and rebuilding it - corruption never crossed my mind.

Thanks again.

-dK
 
Good point Linq. I completely missed that. Thanks.
 

Users who are viewing this thread

Back
Top Bottom