|
Totals for Calculated Values
You cannot use the simple method from the previous lesson for totaling values that come from
a calculated control. There are ways, however, to get around this
limitation!
Method 1: Repeat the calculation in the control source for the total
(Can not use an
aggregate function in the calculation)
- You have a calculated control in Detail section:
The control source is an
expression that calculates the control's value. The calculation does NOT use an aggregate function (Sum, Avg, Count, Max, Min, etc.).
In the example report, the control
Savings is in the Detail section and is calculated with the expression
=[Retail Price]-[Actual Price]
- To hold the total, create an unbound text box control in a Header
or Footer:
As control source, use an expression that performs the total and repeats
the calculation instead of using the name of the calculated
control, like
=Sum([Retail Price]-[Actual Price])
instead of =Sum([Savings])
In the example this expression is used for three controls, two in Group
Footers and one in the Report Footer.

Other aggregate expressions would also work, like
=Max([Retail Price]-[Actual Price]) or
=Avg([Retail Price]-[Actual Price])
Error: Parameter dialog appears
The
expression
=Sum([Savings]) seems logical to total the
Savings values. This syntax worked to total the fields
Retail Price and Actual
Cost. However, if you try it with Savings, a parameter dialog
will ask you to
enter a value for Savings. Since
Savings
was a calculated control, Access cannot find its value to put in a
header or footer.
Method 2: Calculated field in query
- Make the report's source a query which includes a calculated
field.
In the example you could add a column to the source query like:
Difference:[Retail Price]-[Actual Price]
- Create a control in a header or footer for the total based on the
calculated field.
Like =Sum([Difference]) or
=Max([Difference])
Method 3: SUM by collecting a running sum
(Can use an aggregate function in the calculated control)
This method is required when the calculation
includes an aggregate function (Sum, Avg, Max, Min, etc.) but will
also work for simpler calculations. A
Running Sum adds up the values. (Duh!) There is no way to do a
running average, or any other aggregate function. Just adding up the
calculated values.
- Create a calculated control in a Detail, Group Header, or Group
Footer section:
The control source can use a field name or an
expression to calculate the control's value.
The expression is allowed to use an
aggregate function.
Examples:
=[Savings]
=[Retail Price]-[Actual Price]
=Sum([Retail Price]-[Actual Price])
=Avg([Retail Price])
=Max([Retail Price])
=Max([Retail Price]-[Actual Price])
- Set the Running Sum property for the control to
Over Group or Over All.
Now the control will show an accumulating total.
- For Grand Total: If the Running Sum is set to Over All, then you
can repeat the final total in the Report Footer or Header.
- Create an unbound text box
control in the Report Header or Footer.
- Set its control source to be the name of the control
accumulating the running sum.
Example:
If the control RunSumSavings is collecting a running sum, then the control in
the Report Footer would have =[RunSumSavingsl ]
as its control source. This control will show the last value of the
running sum.
To see both Running Sum and
current value:
Use two controls. One with Running Sum = No to show the record's
value and one with Running Sum = Over Group or Over All.
Invisible control: If all you
want to see is a grand total for the group or report, you don't need to
see the accumulating values for each record. Set the Visible property
for the control in the Detail section to No.
Method 4: Create a function - If you used code to calculate a
control's value, you must create your own function to get a total.
Example from Microsoft
Problem:
Totals are not accumulating as expected
- Check the Table Design View to see if your numbers are actually Text
Data Type instead of Number or Currency type.
- Check the order of your choices in the
Sorting and Grouping dialog.
- Check that you are totaling the correct control. (Spelling counts!)
|
Step-by-Step: Totals with Calculated Values |
 |
What you will learn: |
to revise report's SQL using Query Builder
to paste a control's label to different section
to create a calculated control
to create a total of calculated values for group and whole report with:
Method 1: Repeating calculation in Total control
Method 2: Query with calculated field
Method 3: Running Sum property |
Start with:
,
resource files,
worldtravel.mdb, report
Trips by Agent - Total Price from the
previous lesson
Revise Report Source, again
The Price shown in the latest version of the
Trips by Agent report is the "before discount" price. You need
to include in your report the discount percentage and the "after
discount" price. Then you can create some helpful totals.
The report's source does not include the value you need for the
discount. You must edit the source query again.
- If necessary, open the report
Trips by Agent - Total Price that you
created in the
previous lesson.
- Select the whole report.
- In the Properties dialog, open the Query
Builder for the control source of the report.
- Add the table Discounts to
the query.
(Click the Add Table button to open the dialog and select Discounts.)
- Add the field Discount % to the
grid.
(Drag from the Discounts table list and drop on the grid)

- Close the query and save the SQL statement
when prompted.
Do not save the SQL statement as a separate query.
Add Control; Separate Label
Now you can add the Discount % field to the report and create a new
column.
- If necessary, show the Field List by clicking
its button
on the toolbar.
-
Drag
the field Discount % from the Field List
and drop to the right of the Price field in the Detail section.
- Click on the new label and try to drag it up
to the Page Header.
You cannot drag it out of the section.
It would be nice to move this formatted label to the Page Header with
the other column labels instead of deleting it and creating a new
label. There is a way!
- With the label still selected, use the key combo CTRL + X to
delete the label.
The label vanishes.
-
Click
in the Page Header section and paste.
The label appears, but at the far left, not where you clicked. It is
hard to see because it overlaps another label.
-
Drag
the label to the right of the Price label and above the
Discount % control in the Detail section.
- If necessary, align the new label with the other labels in
the Page Header section.
Align the Discount % control with the others in the Detail section.
-
Switch
to Print Preview.
Whoops. The Discount % values are aligned to the right. The column of
values is not under the label.
You can resize the control, since the numbers are going to be
small.
- Switch to Report Design View.
- Resize the Discount % control to about 1/4 inch.

-
Switch
to Print Preview.
Better.
If necessary, change the size or position of the control to
line the values up neatly under the label.
Add Calculated Control and Label
Working with calculated values is a bit trickier. You will calculate the actual price after discount
so that you can add those prices up.
-
If necessary, open the report
Trips by Agent - Total Price in Report Design View.
-
In the Detail section, use the Text Box tool to
create a
new unbound control to the right of the Discount % control.
- Delete the attached label for the new control.
- In the Properties dialog for the new control, type the expression
=[Price]-([Price]*0.01*[Discount %])
Explaining the expression: The numbers in
Discount % are numbers like 5 or 10, not in
percentage format. So we must
multiple the Discount % by 0.01 to get the correct decimal value. The
expression calculates the discount amount with the expression
[Price]*0.01*[Discount %] and then
subtracts it from the Price.
-
In
the Properties dialog, choose Currency for the Format
property.
-
Change the control's name to NetPrice.
- Use the Label tool to create a new label in the Page
Header with the text Net Price.
- Position the new label neatly over the new control.
- Align the label to the other labels in the Page Header
section and the new control to the others in the Detail section.
-
Switch
to Print Preview.
If necessary, return to design view and make corrections.
Save
the report as Trips by Agent - Net Price.
Total of Calculated Values: Using Field Name (Wrong!)
You are now going to create a total for each agent and for the whole
report of the Net Price values. There is a problem with using an
aggregate function like SUM with calculated values.
- Switch to Report Design View.
- In the SFullName footer section, create a new unbound text box
control.
- Edit the label to read: Net Price
Total:
-
In the Properties dialog for the new control:
- Edit the control source to be: =Sum([NetPrice])
similar to what you did for the total for Price.
- Change the name of the control to
AgentTotalNet.
- Set the Format property to Currency.
- Copy the new control and paste it into the Report
Footer.
- Edit the label in the Report Footer to read:
Grand Total Net:
- In the Properties dialog for the new control change the
name of the new control to GrandTotalNet.
-
Switch
to Print Preview.
Problem! A parameter dialog appears, asking for a value for
NetPrice.
You cannot use an aggregate function like SUM with a calculated control
like NetPrice. Access is not smart enough! In the totals control, you must repeat the
calculation that created NetPrice to begin with.
- Click on Cancel to close the parameter dialog.
Method 1: Total of Calculated Values - Repeat Expression
You have now seen what happens when you try the "easy" but wrong way to
get a total. Let's do this correctly now.
-
Edit the control source of the AgentTotalNet
and GrandTotalNet controls to read:
=Sum([Price]-([Price]*0.01*[Discount %]))
Suggestion:
Copy the expression (carefully!) from the NetPrice
control and paste it into the Control Source property.
This
expression uses the aggregate function Sum with the expression that
calculated the Net Price control to start with. (If it weren't for copy
and paste, this would be a pain to retype!)
-
Switch
to Print Preview.
Success!
Save
the report. (Trips by Agent - Net Price)
Method 2: Total of Calculated Values with Query
Doing calculations in the report's query is usually faster than doing
them as the report is formatted. The more records there are, the more
important it is to consider such speed issues.
You will now revise the report to calculate the
NetPrice in the source query.
- Switch to Report Design View.
Save
the report as Trips by Agent - NetPrice Query.
- Select the whole report.
- In the Record Source in the report's Properties dialog, open
the Query Builder.
(Click in the property box and then click on the ellipsis button.)
- Insert a new column. (|)
- Type the definition for a new field in the top row of the new
column:
NetPrice:[Price]-([Price]*0.01*[Discount %])
-
Run
the query to be sure it works.
The datasheet looks good.
But wait. There are 10 trips in this datasheet. The Trips table has 11
records! What happened?
Missing Records: The joins between tables for this query require
that there be values on both sides of the join. This query does not show
Trip #11 because that trip does not have a ClientID
entered in the Trips table. That may or
may not be what you want to happen. There are two easy fixes, depending
on what you want to see. You can change the join to show the trip
anyway, or (better) you can open the Trips
table and enter a ClientID. With the Query
Builder window open, you cannot open another Access object to edit the
data. We will get back to this shortly, once the report has been
changed.
- Close the Query Builder window and save the
changes when prompted.

-
Edit
the control NetPrice in the Detail section to use the new field
NetPrice as its control source.
- Edit the two totals that used the calculation to
use the simple expression =Sum([NetPrice])
AgentTotalNet
GrandTotalNet
-
Switch
to Print Preview.
Success! The report shows the same numbers as before! With so few
records, you certainly would not notice a difference in the time to
format the report, but with many records, using the query would make a
difference.
Save
the report. (Trips by Agent - NetPrice Query)
- Switch to the Database Window.
- Open the table Trips in
datasheet view..
- Change the ClientID for record 11
from 0 to 1.
- Close the table.
- Switch back to the report and open Print Preview
again.
Now the report shows all 11 records with updated totals.

Method 3: Total of Calculated Value (with Running Sum)
There are times when you would like to see the total as it accumulates
and not just a final total. That is done using the Running Sum property.
- Switch to Report Design View.
-
Change
the column label Net Price to Cumulative Total
- Delete the Discount % label in the Page Header
and its control in the Detail section.
- Change the name of the control NetPrice
in the Detail section to CumTotal.
- Change the Running Sum property for CumTotal to
Over Group.
- Add a label to the Page Footer containing your name.

- Switch to Print Preview.
The total accumulates in each group.
Save
the report as Trips by Agent - Cumulative
Total
Print. (1 page)

|