Microsoft KB Archive/208835

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

Article ID: 208835

Article Last Modified on 6/30/2004

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q208835



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

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).



SUMMARY
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


 * 1) 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.

Displaying the Total from a Subreport on a Main Report
CAUTION: If you follow the steps in this example, you modify the sample Access project NorthwindCS.adp. You may want to back up the NorthwindCS.adp file and follow these steps on a copy of the project.

To display the total from a subreport on a main report, follow these steps.  Open the sample database Northwind.mdb or the sample project NorthwindCS.adp, and then open the Sales By Category Subreport report in Design view. Add a Report Footer section by clicking Report Header/Footer on the View menu. Set the Report Header section's Height property to 0 . Add a text box to the Report Footer section, and then set the following properties:

Name: Total

Control Source: =Sum([ProductSales])

Format: Currency

 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 then set the following properties for the text box:

Name: Category Total

Control Source: =[Sales by Category Subreport].[Report]![Total]

Format: Currency

</li> Preview the Sales By Category report. Note that the total of product sales for each category is 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 or the sample project NorthwindCS.adp.</li> Open the Sales By Category Subreport report in Design view.</li> Create a copy of the Sales By Category Subreport report by clicking Save As on the File menu. Save the report as Sales By Category Subreport Summary.</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 then set the following properties for the control:

Name: Sales by Category Subreport Summary

Source Object: Report.Sales by Category Subreport Summary

</li> Preview the report. Note that a grand total appears 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 or the sample project NorthwindCS.adp.</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:

Name: Sales RunningSum

Control Source: =Val([Category Total])

Running Sum: 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> Add a text box control to the Report Footer section to display the grand total. Set the following properties for the control:

Name: Grand Total

Control Source: =[Sales RunningSum]

Format: Currency

</li></ol>

<div class="references_section">