How to refer to a control on a subform or subreport in Access 2000 (209099)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q209099
For a Microsoft Access 97 version of this article, see 113352.
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

SUMMARY

This article describes how to refer to controls on subforms or subreports, and describes some common behaviors you may encounter when you refer to controls on subforms or subreports.

MORE INFORMATION

The Form or Report identifier is optional when referring to control properties. It is necessary, however, when referring to subform or subreport properties.

To refer to a control on a subform, use the following syntax:

Forms![main form name]![subform control name].Form![control name]

To refer to a control on a subreport, use the following syntax:

Reports![main report name]![subreport control name].Report![control name]

NOTE: Only subforms are discussed in the remainder of this article, but all of the information in the article applies to both subforms and subreports.

It is important to note that you cannot refer to controls on a subform with the following syntax:

Forms![subform name]![control name]

This is because a subform on a main form is not a form, but is a control just like a text box or a list box. You must refer to a subform as a control rather than a form, and specify the Form identifier following the subform control name reference to gain access to the controls on a subform.

It is also important to note that when you are referring to controls on a subform, you must specify the value contained in the ControlName property of the subform control. The ControlName property should not be confused with the SourceObject property for a subform control. The SourceObject property is used to indicate which subform to use in the subform control. The ControlName property is used to specify the name by which the subform control is referenced. The ControlName property is typically set the same as the SourceObject property, but it does not have to be.

For example, consider an Order Details subform on an Orders form with the following properties:

Name: Order Details
Control Source: Order Details

You can refer to a Unit Price control on the Order Details subform with the following reference:

Forms![Orders]![Order Details].Form![Unit Price]

If, however, the subform control has the following properties

Name: Details
Control Source: Order Details

you must refer to the Unit Price control on the Order Details subform with this reference:

Forms![Orders]![Details].Form![Unit Price]

Examples of Referencing Controls on a Subform

You may use the following SetValue macro action to increase the Unit Price value on the Orders subform by 10 percent:

Action: SetValue
Item: Forms![Orders]![Order Details].Form![Unit Price]
Expression: Forms![Orders]![Order Details].Form![Unit Price]* 1.1

If the macro is attached to a button on the Orders form, you can use the following expression for the SetValue Expression argument:

Expression: [Order Details].Form![Unit Price]*1.1

If you are referring to a control on a subform from another control on the same subform, you do not have to type the Form property identifier. For example, to refer to the Unit Price value on the Order Details subform in a macro attached to a button on the Order Details subform, you can type:

[Unit Price]

The following expression can be entered as the Control Source property for the Subtotal control on the Orders main form to display a value calculated in the hidden Order Subtotal on the Order Details subform:

=[Orders Subform].Form![Order Subtotal]

To refer to the value of a control on the parent (main) form from a control on a subform, use the Parent property. For example, the following expression typed in a control on a subform refers to the Customer ID field on the parent form:

=Parent![Customer ID]

To refer to a control on a nested subform (a subform on a subform), you can use the following syntax:

Forms![main form name]![subform control name].Form![nested subform control name].Form![control name]

Common Issues Encountered When Referencing Subform Controls

  • If the following error message appears
    Invalid reference to form 'subform name'
    a control on a subform was referenced with the following syntax:

    Forms![subform name]![control name]

    The issue is that the subform is not really a form, but a control that appears on a main form. Refer to the subform as a control, rather than as a form, with the following syntax:

    Forms![main form name]![subform control name].Form![control name]

  • "#Name?" appears in a control with an expression referring to a subform control.

    The ControlName property for the subform control is not what you expect. Open the main form in Design view, select the subform control, and then click Property on the View menu. Compare the ControlName property value to the SourceObject property value.

    The SourceObject property is used to indicate which subform to use in the subform control. The ControlName property is used to specify the name by which the subform control is referenced. The ControlName property is typically set the same as the SourceObject property, but it does not have to be.

    NOTE: This behavior may also cause the following error message to appear:
    Invalid reference to field 'subform name'

REFERENCES

For more information about controls, click Microsoft Access Help on the Help menu, type controls: what they are and how they work in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Modification Type:MajorLast Reviewed:7/28/2004
Keywords:kbhowto kbinfo KB209099