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

ID: Q119134


The information in this article applies to:


SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

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:

Q150895 ACC95: Microsoft Access Sample Forms Available on MSL

Q175066 ACC97: Microsoft Access 97 Sample Forms Available on MSL


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.

Additional query words: totals


Keywords          : kbusage FmsHowto 
Version           : 1.0 1.1 2.0 7.0 97
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 6, 1999