Microsoft KB Archive/129721

= ACC: How to Display and Total Subtotals from Subreports =

Article ID: 129721

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q129721



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

This article demonstrates how you can use a total from a subreport on a main report and how you can display a total for all the subreports.



MORE INFORMATION
If you use an expression similar to   =Sum([MySubReport].Report![Product Sales]) on a main report to sum a reference to a subreport control that contains a subtotal, you receive a "#Name?" error message. This error message occurs because the Sum function (and the other aggregate functions) can reference only fields, not controls. To avoid this error, place the sum expression in the subreport's report footer, and then reference that expression in the main report.

CAUTION: Following the steps in the examples below will modify the sample database Northwind.mdb (or the NWIND.MDB in version 2.0). You may want to back up the Northwind.mdb or NWIND.MDB file or perform these steps on a copy of these databases.

Displaying the Total from a Subreport on a Main Report
To display the total from a subreport on a main report, follow these steps.  Open the sample database Northwind.mdb, and open the Sales By Category Subreport report in Design view. Add a report footer section by clicking Report Header/Footer on the View menu.

NOTE: In version 2.0, click Report Header/Footer on the Format menu. Set the report header section's Height property to 0.  Add a text box to the report footer section and set the following properties:      Name: Total ControlSource: =Sum([ProductSales]) Format: Currency

NOTE: In version 2.0, there is a space in Product Sales.  Set the report footer section's Visible property to No. Save and then close the report.</li> Open the Sales By Category report in Design view.</li>  Add a text box to the Category Name header section below the subreport and set the following properties for the text box: <pre class="fixed_text">     Name: Category Total ControlSource: =[Sales by Category Subreport].[Report]![Total] Format: Currency </li> Preview the Sales By Category report. Note that the total product sales for each category are displayed below the listing of sales per product.</li></ol>

Displaying a Total for All Subreports
To display a grand total for all subreports, you can use either a second subreport or a control's RunningSum property.

Using a Second Subreport:

You can use a second subreport to display a grand total. When you add a second subreport to a group footer section, make sure to link the subreport to the grouped field so that the subreport displays the total for that group. If you place the subreport in the report footer section, do not link the subreport to the grouped field so that the subreport displays a total for all the records.

The following example demonstrates how to use a second subreport to display a grand total on a main report: <ol> Open the sample database Northwind.mdb.</li> Open the Sales By Category Subreport report in Design view.</li> Create a copy of the Sales By Category Subreport report clicking Save As/Export on the File menu. Save the report as Sales By Category Subreport Summary.

NOTE: In version 2.0, click Save As on the File menu.</li> Set the detail section's Visible property to No.</li> Set the report footer section's Visible property to Yes.</li> Save and then close the Sales By Category Subreport Summary report.</li> Open the Sales By Category report in Design view.</li>  Add a Subform/Subreport control to the report footer section and set the following properties for the control: <pre class="fixed_text">     Name: Sales by Category Subreport Summary SourceObject: Report.Sales by Category Subreport Summary </li> Preview the report. Note that the total sales per category and a grand total appear on the last page.</li></ol>

Using the RunningSum property:

To use the RunningSum property to display a grand total on a main report, follow these steps: <ol> Open the sample database Northwind.mdb.</li> Open the Sales By Category report in Design view.</li>  Add a text box that references the text box you created in step 8 of the "Displaying the Total from a Subreport on a Main Report" section above. Set the following properties for the text box: <pre class="fixed_text">     Name: Sales RunningSum ControlSource: =Val([Category Total]) RunningSum: Over All Visible: No NOTE: You use the Val function to convert the Variant returned by the referenced control to a number so that the RunningSum property accumulates the total. </li> <li> Add a text box control to the report footer section to display the grand total. Set the following properties for the control: <pre class="fixed_text">     Name: Grand Total ControlSource: =[Sales RunningSum] </li></ol>

<div class="references_section">