Set focus to textbox in subform after clicking command button

cmray58

Registered User.
Local time
Today, 16:19
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
 
Perhaps you have to set focus to the subform control first or perhaps you need to use Docmd.GoToControl first
 
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.
 
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.
 
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:
I have this form / subform issue. I have followed the guidance given here. When the form loads I set the focus to the subform and then to a textbox in the subform (as recommended) On the screen the text box in the subform is highlighted (reverse colours) to indicate it has the focus, but if I type a new value or press the tab key nothing happens. Only if I move the mouse to the box and click does it "wake up" and let me type. The text box is not locked and it is Enabled
 
what code does you have?
i strongly suggests post #5 code.
on the Load Event of your MainForm:
Code:
Private Sub Form_Load()
With DoCmd
    .GotoControl "yourSubformName"
    .GotoControl "textboxNameOnYourSubform"
End With
End Sub
 
what code does you have?
i strongly suggests post #5 code.
on the Load Event of your MainForm:
Code:
Private Sub Form_Load()
With DoCmd
    .GotoControl "yourSubformName"
    .GotoControl "textboxNameOnYourSubform"
End With
End Sub
Arnel,
Wouldn't that be the subform control name?
 
This is (now) the code in the Load event of the main form
Code:
[Forms]![frmTelephoneSchedule]![Child4].SetFocus
[Forms]![frmTelephoneSchedule]![Child4].Form![Quantity].SetFocus

DoCmd.GoToControl "Child4"
DoCmd.GoToControl "Quantity"

But the addition of the two GoTo lines has not changed the problem. The focus "appears" to be on Quantity - but pressing a number on the keyboard (or any other key e.g. TAB) does not work. It needs a mouse click on it to activate it???
 
are there "other" codes on your main/subform aside from the Load event of your main form?
are there other subforms on Main form?
 
I think I have found the issue:
Because of the form potentially opening behind the form that calls it I put the following at the start of the Form Load event
Code:
'Call BringWindowToTop(Me.hwnd)

I have now removed this line and all is now good.
Not sure I can explain why this line is having the effect on inhibiting the key board on the sub form.
Now I will just have to hope that the form doesn't hide itself behind :-)
 
what code does you have?
i strongly suggests post #5 code.
on the Load Event of your MainForm:
Code:
Private Sub Form_Load()
With DoCmd
    .GotoControl "yourSubformName"
    .GotoControl "textboxNameOnYourSubform"
End With
End Sub

There is no reason to go though DoCmd, instead use the controls directly. Assuming your sub-form's control name is Form6 and the control on the sub-form is Provider_Decision

Code:
With Me.Form6
    .SetFocus
    .Provider_Decision.SetFocus
End With
 
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
Try setting the focus to the subform itself first and then to the subforms control like this (assuming Form6 is the subform):

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

Users who are viewing this thread

Back
Top Bottom