Setting focus to a control on a subform on opening

J

John S. Ford, MD

In Access 97, have a form with a subform on it. Upon opening the main
form, I'd like the focus to be immediately set to a textbox on the subform.
The name of the subform control (not the actual subform is sbfctlSubForm.
(In case it matters, the name of the actual subform that was embedded into
the main form is sbfSubForm). I've tried the following which doesn't work:

Private Sub Form_Open(Cancel As Integer)
Me!sbfctlSubForm.Form!txtControl.SetFocus
End Sub

What am I doing wrong?

John
 
A

Allen Browne

You have to set focus to the subform control first, before you will see it
set focus to the control in the subform.

You may also find that Form_Open is too early. Try:

Private Sub Form_Load()
Me.sbfctlSubForm.SetFocus
Me.sbfctlSubForm.Form!txtControl.SetFocus
End Sub
 
J

John S. Ford, MD

Allan,

That worked perfectly. In fact, since txtControl was the first control in
the tab order of my subform, I didn't even need to use the second line of
your code. I did test it with other controls on the subform and it worked
perfectly as written (when placed in either the OnLoad event or the OnOpen
event).

I don't need a response but just curious: any idea as to why I couldn't
access the textbox focus directly from line two? Is the inability to do
that documented in Access 97?

Thanks for the help,
John
 
A

Allen Browne

Access keeps track of the control that has focuse in each form
independently, so setting focus to something in the subform without making
it the active control in the main form does not achieve what you wanted.
 

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