Introduction.
How to bring the sub-report summary value to the main report and use it in calculations?
But first, we need a ready-made report for our project.
Download links for the sample database are provided below. Choose the version you need. The demo database is created in Microsoft Access 2007 format and is fully compatible with later versions as well.
First, take a look at the following images:
- Print Preview of the finished report.
- Original Report Preview.
Changes were made to the Report in Design View to get the result shown in the first image above.
Download Links.
- Sample Database Download Links for Microsoft Access 2007 and 2003 Versions are given below.
- Download the suitable sample database.
In short, our task is to add the sub-report category total to the Footer Section of the Sales by Category sub-report. Get the summary value of each category for the main report header. Calculate the percentage of each category of product sales value on Grand-Total Sales Value (or Percentage = Category Sales Value / Total Sales value of all Categories * 100). We can do this by adding a few text boxes to both reports and writing a few expressions in them.
The Sub-Report Changes.
Let us start with the Sales by Category, Sub-report first.
Open the downloaded database.
Open Sales by Category, Sub-report in design view.
Right-click on the Report Footer bar and select Properties to display the Property Sheet.
Select the Height Property and change the property value to 0.33” or 0.838 cm.
Select the Text Tool from the Toolbar above and draw a text box on the Report Footer area below the Product Sale column.
Write the expression =Sum([ProductSales]) in the Control Source property of the text box. Change the Name property value to SubTotal.
Modify the Caption property value of the child label to read as Sub-Total.
Save and close the Sales by Category Subreport.
The Main-Report Changes.
Open the Sales by Category main report in Design View.
Create a Text Box, on the Header Section of the Report, to the right of the report heading.
While the text box is in the selected state, display the property sheet (F4).
Write the expression =Sum([ProductSales]) in the Control Source property. Change the Name property value to TotalSales. Change the Caption property value of the child label to Total Sales.
Note: The above expression calculates the total product sales value across all categories on the main report. When used in the sub-report, however, the same expression calculates the sales value of the current product category (for example: Beverages). This gives us two values: the SubTotal for a specific category in the sub-report, and the TotalSales for all categories in the main report.
With these values, we can calculate the percentage contribution of a particular category using the formula:
However, since SubTotal is calculated in the sub-report, it cannot be directly referenced in the parent (main) report. To use it in the main report—especially when there are multiple sub-reports—we must explicitly specify the location of the control or expression within the sub-report.
Create a text box below the Category Header bar to the right of the Category Name heading, move the text box to the right, and position it under the Total Sales calculation control on the Report Header.
Right-click on the Control Source Property of the text box and select the Build option from the displayed list to open the expression builder control.
Click on the = symbol to insert it into the expression editor window.
Double-click on the + symbol on the left side of the Reports option to expand and show other options.
Double-click on Loaded Reports.
Double-click on the Sales by Category main report to display the Sub-Report's name.
Click on Sales by Category Subreport to display its control names in the next column.
Find the Subtotal control in the list and double-click on it.
The reference to the SubTotal control in the sub-report can be written as:
This reference can be inserted directly using the Expression Builder. Alternatively, if you already understand how to write the reference correctly, you can type it directly into the Control Source property without going through the Expression Builder.
With practice and by studying Microsoft Access’s addressing conventions, you will quickly become comfortable writing these references manually for use in Reports or Forms.
Click OK to come back to the Control Source property of the text box with the reference of the subtotal control and to write the rest of the expression to calculate the percentage.
Type /[TotalSales] at the end of the subtotal reference. Have you noticed the slash on the left side of the expression snippet?
Select Percent from the drop-down list of the Format property. With these settings, we don’t have to write *100 part in the expression.
Change the Caption of the child label to Category %. Change the font size of the Text box and child label to 12 points and make Font-weight Bold.
Report Sample Print Preview
Our Report is almost finished, but we need a little more change, and that comes next after we preview the progress of our work so far
Save the changes we have made in the Report and open it in Print Preview to see the result of the changes we have made to the report so far.
The Report should look like the image below.
Move the Report to the next page.
The Total Sales value of the Report Header Section is not appearing on the second page.
By default, the Report Header section prints only on the first page of the report, and the Report Footer section prints only on the last page. This means that any headings or calculations placed in the Report Header will not appear on subsequent pages.
However, we often want the report heading and certain calculated values to appear on every page. Content placed in the Page Header section is repeated across all pages, making it the ideal place for such information.
The challenge is that aggregate functions like SUM(), Count(), and similar expressions do not work in the Page Header section. For example, we cannot directly place a SUM() calculation there. But we do want the calculated value from the Total Sales control (defined in the Report Footer or another valid section) to appear consistently at the top of every page.
To achieve this, we make the following adjustments:
Move the report heading from the Report Header to the Page Header, so it prints on every page.
Reference the Total Sales control (which performs the calculation in a valid section, such as the Report Footer) from within the Page Header. By pointing to the existing Total Sales control, its value can be displayed in the Page Header even though aggregate functions cannot be calculated there directly.
With this approach, both the heading and the calculated Total Sales value will appear consistently on every page of the report.
The Final Changes.
Open the report in design view.
Drag the Category Name header bar down to get enough space for the Page Header Section, to cut and paste the Report Heading there.
The highlight report heading and the report date controls (leave alone the Total Sales textbox), cut, and paste into the Page Header Section.
Select the Total Sales text box, copy and paste it into the Page Header Section, and move the position below the text control on the Report Header Section.
Write the expression = [TotalSales] (the Name of the total sales calculation text box on the header section) in the Control Source property (overwriting the existing expression) of the copied text box.
This will display the value of the Header Section text box, where the Total Sales value is calculated.
Select the Header Section text control along with the child label and set their Visible Property value to No, to keep it hidden when the report is previewed or printed.
Save the report with the changes.
Print Preview the Report.
Open the Sales by Category report in Print Preview, move the pages forward, and check the headings and category percentage values.
Next time you want to do something like this, you can do it in a few minutes.
No comments:
Post a Comment
Comments subject to moderation before publishing.