ACC: How to Bring a Subtotal from a Subform to a Main Form

Article translations Article translations
Article ID: 119134 - View products that this article applies to.
This article was previously published under Q119134
Moderate: Requires basic macro, coding, and interoperability skills.
Expand all | Collapse all

SUMMARY

This article demonstrates how to correctly reference a subtotal control on a subform from a main form.

NOTE: This article explains a technique demonstrated in the sample files, FrmSampl.exe (for Microsoft Access for Windows 95 version 7.0) and FrmSmp97.exe (for Microsoft Access 97). For information about how to obtain these sample files, please see the following articles in the Microsoft Knowledge Base:

150895 ACC95: Microsoft Access Sample Forms Available in Download Center

175066 ACC97: Microsoft Access 97 Sample Forms Available in Download Center

MORE INFORMATION

If you try to sum a subform field using a calculated control on a main form, you may receive an error message in the main form control. For example, the following expression in a main form control generates a "#Name?" error message:
=Sum([MySubForm].Form![ExtendedPrice])

To avoid an error, you can sum the subform field using a calculated control in the subform's form footer. You can then reference the calculated control from the main form to display the summed value.

This technique is demonstrated in the Orders form of the sample database Northwind.mdb in Microsoft Access 7.0 and 97 and NWIND.MDB in Microsoft Access 1.x and 2.0. The Orders Subform contains a calculated control in the subform's form footer. This calculated control sums the subform's Extended Price field to create a subtotal using the following syntax:

In Microsoft Access 7.0 and 97:
   Text Box Control

      Name: OrderSubtotal
      ControlSource: =Sum([ExtendedPrice])
				

In Microsoft Access 1.x and 2.0:
   Text Box Control

      Name: Order Subtotal
      ControlSource: =Sum([Extended Price])
				

Because the subform's DefaultView property is set to Datasheet, you do not see the calculated control in the subform's form footer when you open the Orders form in Form view. However, a text box control on the main form refers to the subform's calculated control and displays its value. The main form control is called Subtotal and references the subform's calculated control using the following syntax:

In Microsoft Access 7.0 and 97:
   Text Box Control

      Name: Subtotal
      ControlSource: =[OrdersSubform].[Form]![OrderSubtotal]
				

In Microsoft Access 1.x and 2.0:
   Text Box Control

      Name: Subtotal
      ControlSource: =[Orders Subform].[Form]![Order Subtotal]
				

This reference allows the main form to display a subtotal of a subform field while the actual totaling of subform values is performed using a calculated control in the subform's form footer.

REFERENCES

For more information about referencing subform controls, search for "subforms," then "referring to in expressions," and then "Calculate a total in a subform and display it on a form" using the Microsoft Access Help Index.

Properties

Article ID: 119134 - Last Review: January 19, 2007 - Revision: 2.1
APPLIES TO
  • 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
Keywords: 
kbhowto kbusage KB119134
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.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com