Set focus to textbox in subform after clicking command button (1 Viewer)

cmray58

Registered User.
Local time
Today, 00:56
Joined
Mar 24, 2014
Messages
70
I have a command button set to navigate to the next record (Docmd.gotorecord etc..) but then I want to focus on a textbox that's in a subform so the user can begin typing after clicking the command button without having to then click on the textbox.

I have

[Forms]![Provider_Review_Main_Form].[Form6].[Form].[Provider_Decision].SetFocus

With no error but it's not working
 

bob fitz

AWF VIP
Local time
Today, 08:56
Joined
May 23, 2011
Messages
4,726
Perhaps you have to set focus to the subform control first or perhaps you need to use Docmd.GoToControl first
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:56
Joined
Feb 28, 2001
Messages
27,332
If I recall correctly, you might be able to identify the control that actually has the focus by trying to capture its name. Screen.ActiveControl.Name should be the thing that gained control. Further, if you were in doubt, Screen.ActiveControl.Parent.Name should be the form holding the control that has focus. So if you put a couple of string variables in that module and loaded them with those two names and then put in a breakpoint AFTER those names were loaded, you could do a Debug.Print on the variables to see the form name and control name having focus.

SetFocus doesn't always work as expected. Don't forget that your command button will attempt to undergo a "LostFocus" event if you try to set focus elsewhere. That is, if your button_Click event does the SetFocus, you will still have to undergo the LostFocus. Does the button have an active LostFocus event?

If it were left to me, I would maybe put a breakpoint on the line of code that does a SetFocus where you wanted it, then single-step to see what routines fire and in what order. I guarantee you have not less than five potential events pending if that is as you described it. The button's LostFocus, the parent form's Exit, the child form's Enter and Activate (or maybe it is Activate and Enter, I can never remember which one comes first), and the child form's control's GotFocus. If any one of those events does things to either form, that is where your focus went.
 

jdraw

Super Moderator
Staff member
Local time
Today, 03:56
Joined
Jan 23, 2006
Messages
15,396
I agree with Doc_Man--- try a break point and then step through your code/logic.

Have your Locals window open, or make use of some debug.prints to see if values in controls are what you expect.

Good luck.
 

apr pillai

AWF VIP
Local time
Today, 13:26
Joined
Jan 20, 2005
Messages
735
Setting focus to a field on a sub-form is a two step task. First, we must set focus to the sub-form control. Then we can set focus to the field, as you were trying to do in your command.

The following link explains this method in detail: Setting Focus on a field inside a Sub-Form
 
Last edited:

Users who are viewing this thread

Top Bottom