Using Controls(variable_name).SetFocus on a subform

G

Guest

I'm attempting to set the focus to a control on a subform if one of the
fields is blank. The field name to set focus to is stored in the variable
FirstError. The code I'm trying to use is:

If Check = False Then
Me.Controls(FirstError).SetFocus
Exit Sub
End If

In the immediate window, the FirstError variable displays the correct
control name on the subform, but the syntax is incorrect and can't find the
control to set focus to. Any ideas?

Thanks,
Melanie
 
P

PC Datasheet

Assuming the code is in the subform's module, simply put double quotes
around FirstError.
 
G

Graeme Richardson

Hi Melanie,
This code might be failing because you're viewing the code window (in break
mode) and not the form when the SetFocus statement is executed.
Try executing the code without a break.

HTH, Graeme.
 
J

John Vinson

I'm attempting to set the focus to a control on a subform if one of the
fields is blank. The field name to set focus to is stored in the variable
FirstError. The code I'm trying to use is:

If Check = False Then
Me.Controls(FirstError).SetFocus
Exit Sub
End If

In the immediate window, the FirstError variable displays the correct
control name on the subform, but the syntax is incorrect and can't find the
control to set focus to. Any ideas?

Thanks,
Melanie

You need to setfocus *twice*: once to the Subform itself, tehn to the
control. For one thing, the controls on a Subform are not members of
the mainform's Controls collection.

Try

Me!subMySubform.SetFocus
Me!subMySubform.Form.Controls(FirstError).SetFocus


John W. Vinson[MVP]
 
G

Guest

John,

Thanks for the help. I tried the code you suggested below. However, I'm
now getting an error that reads: "Microsoft Access can't find the field
'subfrm_name' referred to in your expression." The code is executed from
within the subform's module. Is there something else I can try?

Thanks,
Melanie
 
J

John Vinson

Thanks for the help. I tried the code you suggested below. However, I'm
now getting an error that reads: "Microsoft Access can't find the field
'subfrm_name' referred to in your expression." The code is executed from
within the subform's module. Is there something else I can try?

If the code is executing on the subform then your focus is already on
the subform! Just leave that line out.

John W. Vinson[MVP]
 
G

Guest

Thanks, John. I figured out the problem.

John Vinson said:
If the code is executing on the subform then your focus is already on
the subform! Just leave that line out.

John W. Vinson[MVP]
 

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