SetFocus in a subForm

G

Guest

I am trying to validate some fields in a subform within a form from code
called from the parent form by referring to the subform fields using the
subform.Controls(n) syntax - fine - the validation works OK.
If I do find a subform field which does not meet validation criteria, I want
to highlight the subform field by setting focus (so the User knows which one
to fix). This is the problem - if I do this in the code, when the code
returns to the parent form, the focus is not apparent. OK here is a sample of
code :-

If IsNull(Me.subfrmTraining1.Controls(7)) Then
MsgBox "Please supply Grade"
Me.subfrmTraining1.Controls(7).SetFocus
........


Hope I expained this OK - It's the Me.subfrmTraining1.Controls(7).SetFocus
that doesn't seem to be working.

Any ideas?

Thanks.
 
K

Ken Snell [MVP]

To set focus to a control in a subform, you must first set focus to the
subform control (the control holding the "subform" on the main form), then
set focus to the control in the subform:

If IsNull(Me.subfrmTraining1.Controls(7)) Then
MsgBox "Please supply Grade"
Me.subfrmTraining1.SetFocus
Me.subfrmTraining1.Form.Controls(7).SetFocus

Note I also inserted ".Form" in the last step so that your code will know
it's a control on the subform.
 
G

Guest

Your instructions make sense, but I am having trouble implementing them. My
form has a subform placeholder subDetail which is populated based upon a
dialog box selection. My code successfully populates the placeholder when
intDetailType = 1. It encouters an error from the .SetFocus instruction,
"Run-time error '2210': Microsoft Office Access can't move the focus to the
control subDetail." I can tab to or click on the subform control, but that
is inelegant (and inefficient for the user). Please help!

With subDetail
Select Case intDetailType
Case 1
.SourceObject = "fsubFlight_entry"
.SetFocus
!txtConfirmation.SetFocus
Case 2
.SourceObject = "fsubRCarHotel"
Case Else
MsgBox "Not defined"
End Select
.Visible = True
End With
 

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