You are currently offline, waiting for your internet to reconnect

ACC: How to Refer to a Control on a Subform or Subreport

This article was previously published under Q113352
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Moderate: Requires basic macro, coding, and interoperability skills.
SUMMARY
This article describes how to refer to controls on subforms or subreports,and describes some common problems you may encounter when you refer tocontrols on subforms or subreports.
MORE INFORMATION
NOTE: In Microsoft Access 7.0 and 97, the "Form" or "Report" identifier isoptional 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 rest of this article, but all theinformation applies to both subforms and subreports.

It is important to note that you cannot refer to controls on a subformwith the following syntax:
   Forms![subform name]![control name]				

This is because a subform on a main form is not a form, but is a controljust like a text box or a list box. You must refer to a subform as acontrol rather than a form, and specify the Form identifier following thesubform 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 asubform, you must specify the value contained in the ControlName propertyof the subform control. The ControlName property should not be confusedwith the SourceObject property for a subform control. The SourceObjectproperty is used to indicate which subform to use in the subform control.The ControlName property is used to specify the name by which the subformcontrol is referenced. ControlName is typically set the same as theSourceObject, but it does not have to be.

For example, consider an Order Details subform on an Orders form with thefollowing properties:
   ControlName: Order Details   SourceObject: Order Details				

You can refer to a Unit Price control on the Order Details subform with thefollowing reference:
   Forms![Orders]![Order Details].Form![Unit Price]				

If, however, the subform control has the following properties
   ControlName: Details   SourceObject: Order Details				

you must refer to the Unit Price control on the Order Details subformwith this reference:
   Forms![Orders]![Details].Form![Unit Price]				

Examples of Referencing Controls on a Subform

You could use the following SetValue macro action to increase the UnitPrice value on the Orders Subform by 10 percent:
   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 usethis expression for the SetValue Expression argument:
   [Order Details].Form![Unit Price]*1.1				

If you are referring to a control on a subform from another control on thesame subform, you do not have to enter the Form property identifier. Forexample, to refer to the Unit Price value on the Order Details subform in amacro attached to a button on the Order Details subform, you can enter:
   [Unit Price]				

The following expression can be entered as the ControlSource property forthe Subtotal control on the Orders main form to display a value calculatedin the hidden Order Subtotal control 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 controlon a subform, use the Parent property. For example, the followingexpression entered in a control on a subform refers to the Customer IDfield 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 Problems Encountered When Referencing Subform Controls


  • The following error message occurs:
    Invalid reference to form '<subform name>'

    This means that you tried to reference a control on a subform with the following syntax:
    Forms![subform name]![control name]

    The problem 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.

    This can occur when the ControlName property for the subform control is not what you expect. Open the main form in Design view, select the subform control, then choose the Property window from 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 is typically set the same as the SourceObject, but it does not have to be.
  • The following error message occurs:
    Invalid reference to field '<subform name>'

    This error message is caused by the same the problem as the problem in the last example.
REFERENCES
For more information about controls, type "controls" in the OfficeAssistant, click Search, and then click to view "Controls: What they areand how they work."
Properties

Article ID: 113352 - Last Review: 01/18/2007 23:39:47 - Revision: 2.1

  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition
  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition
  • kbhowto KB113352
Feedback