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
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.
- Open the Sales By Category report in Design view.
- 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
- 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.
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:
- Open the sample database Northwind.mdb or the sample project NorthwindCS.adp.
- Open the Sales By Category Subreport report in Design view.
- 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.
- Set the Detail section's Visible property to No.
- Set the Report Footer section's Visible property to Yes.
- Save and then close the Sales By Category Subreport Summary report.
- Open the Sales By Category report in Design view.
- 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
- Preview the report. Note that a grand total appears on the last page.
Using the RunningSum property:
To use the
RunningSum property to display a grand total on a main report, follow these steps:
- Open the sample database Northwind.mdb or the sample project NorthwindCS.adp.
- Open the Sales By Category report in Design view.
- 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.
- 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