Setting the focus on subforms

G

Guest

I have a form (frmOcccurance) that has several subforms. I am trying to move
the focus from the field 'Contact' of the subform (sfmContacts) to the field
'CorrIndv' of the subform (sfmCorrections) when there is nothing typed into
the field 'Contact'.
The code I am using is below:

Private Sub Contact_LostFocus()
If Me.Contact.Text = "" Then
Forms!frmOccurance!sfmCorrections.Form!CorrIndv.SetFocus
End If
End Sub

I do not get an error, but the focus just moves to the second field on
sfmContacts. I am assuming hat I am not referencing the subform correctly,
but I am not sure how else to do it. I found the reference used above in the
Access help. Any help would be appreciated.

Steve
 
K

Ken Snell [MVP]

Number one, you cannot change where the focus goes in the LostFocus event.
It's too late at that point. You should use the Exit event instead.

Number two, to set focus on a control in a subform, you first must set focus
on the subform control that holds that focus, then on the control within
that subform. And, if you're wanting to do this from another subform, I
believe you'll need to set focus on a control on the main form first, then
on the subform control, then on the control on the subform.
 
G

Guest

Ken

Thanks for the help. I accidentally found a much simpler method after
reading your post. After trying several strings to reference the individual
field without success, I started testing references to the various controls
and found that by just referencing the control that holds the subform, the
focus would be set to the first field on the subform.

Thanks again,
Steve
 
K

Ken Snell [MVP]

That will work so long as the user hasn't already been in that subform and
put the focus on another control, then clicked to another part of the form
that wasn't part of that subform. The next time that subform gets the focus,
that control that last had focus in that subform will still have the focus;
it won't automatically go back to the first control.
 
G

Guest

I did not think about that happening.

Ken Snell said:
That will work so long as the user hasn't already been in that subform and
put the focus on another control, then clicked to another part of the form
that wasn't part of that subform. The next time that subform gets the focus,
that control that last had focus in that subform will still have the focus;
it won't automatically go back to the first control.
 

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