View Full Version : Calculate the Total field from a query and place result in report


JohnLee
07-09-2008, 06:27 AM
Hi folks,

I'm hoping someone can help me here. I have a report that has an unbound object, which needs to sum up the lngTotal field from a query. However the expression I have entered returns Error everytime I ran the report. This is what I have:

A query called qryNoAddressStats and has the following fields:

strCoyType
lngADDB
lngADDD
lngADD0
lngADD1
lngADD3
lngTotals

The grid view looks like this

Coy Type ADDB ADDD ADD1 ADD3 Totals
DGI1 0 0 1 2 3
DGS1 1 1 0 1 3

What I want to do is to have the totals field summed up and placed in the unbound Text Box [txtNoAddressDailyStats] in my report. This is what I have written in the control source property of the txtNoAddressDailyStats text box:

=Sum([qryNoAddressDailyStats]![lngTotals])

whenever I run the report I get an error in the unbound Text Box.

I am clearly doing something wrong, and I am having trouble working out what that is, any assistance would be most appreciated.

John Lee

Brianwarnock
07-09-2008, 06:51 AM
Itake it that the record soutce for the report is that query?

Brian

JohnLee
07-09-2008, 06:57 AM
Hi,

No, it's an add on to the report, which pulls information from elsewhere, which is why it's unbound.

I have other unbound fields on the report that pull results in as follows:

=DCount("*", "qryDDMandateImportDGI_DailyCount","[strLetterCode]Like ""DDIG""")

These all work fine, because they are counting every record in that particular query for that particular letter code.

This unbound text box needs to sum up the totals field for each total.

Your assistance appreciated.

John

Brianwarnock
07-09-2008, 07:00 AM
Yes but that uses a Domain function, you will need to do the same again, probably DSum.

Brian

JohnLee
07-09-2008, 07:06 AM
Hi,

I tried DSum, keept getting the message "The expression you entered has a function containing the wrong number of arguments".

I don't know what the arguments are, so perhaps this is the problem when trying to use the DSum Function.

If you could help, I would be grateful.

John

Brianwarnock
07-09-2008, 07:12 AM
From Access help

DSum Function
See Also Specifics
You can use the DSum functions to calculate the sum of a set of values in a specified set of records (a domain). Use the DSum function in Visual Basic, a macro, a query expression, or a calculated control.

For example, you could use the DSum function in a calculated field expression in a query to calculate the total sales made by a particular employee over a period of time. Or you could use the DSum function in a calculated control to display a running sum of sales for a particular product.

DSum(expr, domain, [criteria])

The DSum function has the following arguments.

Argument Description
expr An expression that identifies the numeric field whose values you want to total. It can be a string expression identifying a field in a table or query, or it can be an expression that performs a calculation on data in that field. In expr, you can include the name of a field in a table, a control on a form, a constant, or a function. If expr includes a function, it can be either built-in or user-defined, but not another domain aggregate or SQL aggregate function.
domain A string expression identifying the set of records that constitutes the domain. It can be a table name or a query name for a query that does not require a parameter.
criteria An optional string expression used to restrict the range of data on which the DSum function is performed. For example, criteria is often equivalent to the WHERE clause in an SQL expression, without the word WHERE. If criteria is omitted, the DSum function evaluates expr against the entire domain. Any field that is included in criteria must also be a field in domain; otherwise, the DSum function returns a Null.


Remarks

JohnLee
07-09-2008, 07:12 AM
Hi Brian,

Have managed to work out using the DSum Function how to get the desired result, Thanks for your assistance.

John

Brianwarnock
07-09-2008, 07:14 AM
Happy to have helped, especially pleased that you didn't need last post.

Brian