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

Last reviewed: October 24, 1997
Article ID: Q119134
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0, 7.0, 97

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:

   ARTICLE-ID: Q150895
   TITLE     : ACC95: Microsoft Access Sample Forms Available on MSL

   ARTICLE-ID: Q175066
   TITLE     : 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 : FmsHowto kbusage
Version : 1.0 1.1 2.0 7.0 97
Platform : WINDOWS
Hardware : x86
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 24, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.