You can use the DSum function to calculate the sum of a set of values in a specified set of records (a domain (domain: A set of records that is defined by a table, a query, or an SQL expression. Domain aggregate functions return statistical information about a specific domain or set of records.)). Use the DSum function in a Visual Basic for Applications (VBA) module, a macro (macro: An action or a set of actions that you can use to automate tasks. Macros are recorded in the Visual Basic for Applications programming language.), a query expression, or a calculated control (calculated control: A control that is used on a form, report, or data access page to display the result of an expression. The result is recalculated each time there is a change in any of the values on which the expression is based.).
For example, you can 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 can 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 syntax has these arguments:
||Required. An expression that identifies the numeric field whose values you want to total. It can be a string expression (string expression: An expression that evaluates to a sequence of contiguous characters. Elements of the expression can be: functions that return a string or a string Variant (VarType 8); a string literal, constant, variable, or Variant.) 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.
||Required. 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.
||Optional. A 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 (Null: A value you can enter in a field or use in expressions or queries to indicate missing or unknown data. In Visual Basic, the Null keyword indicates a Null value. Some fields, such as primary key fields, can't contain a Null value.).
If no record satisfies the criteria argument or if domain contains no records, the DSum function returns a Null.
Whether you use the DSum function in a macro, module, query expression, or calculated control, you must construct the criteria argument carefully to ensure that it will be evaluated correctly.
You can use the DSum function to specify criteria in the Criteria row of a query, in a calculated field in a query expression, or in the Update To row of an update query (update query: An action query (SQL statement) that changes a set of records according to criteria (search conditions) that you specify.).
Note You can use either the DSum or Sum function in a calculated field expression in a totals query (totals query: A query that displays a summary calculation, such as an average or sum, for values in various fields from a table or tables. A totals query is not a separate kind of query; rather, it extends the flexibility of select queries.). If you use the DSum function, values are calculated before data is grouped. If you use the Sum function, the data is grouped before values in the field expression are evaluated.
You might want to use the DSum function when you need to display the sum of a set of values from a field that is not in the record source for your form or report. For example, suppose you have a form that displays information about a particular product. You can use the DSum function to maintain a running total of sales of that product in a calculated control.
If you need to maintain a running total in a control on a report, you can use the RunningSum property of that control, if the field on which it is based is included in the record source for the report. Use the DSum function to maintain a running sum on a form.
Note Unsaved changes to records in domain are not included when you use this function. If you want the DSum function to be based on the changed values, you must first save the changes by either clicking Save Record in the Records group on the Home tab, moving the focus to another record, or by using the Update method.
Use the DSum function in an expression You can use a domain function (such as DSum) in the Update To row of an update query. For example, suppose you want to track current sales by product in a Products table. You could add a new field called SalesSoFar to the Products table, and run an update query to calculate the correct values and update the records. To do this, create a new query based on the Products table, and on the Design tab, in the Query Type group, click Update. Add the SalesSoFar field to the query grid, and type the following in the Update To row:
"[Quantity]*[UnitPrice]", "Order Details", _
"[ProductID] = "& [ProductID]
When you run the query, Microsoft Office Access 2007 calculates the total amount of sales for each product, based on information from an Order Details table. The sum of sales for each product is added to the Products table.
Use DSum in VBA code
Note Examples that follow demonstrate the use of this function in a Visual Basic for Applications (VBA) module. For more information about working with VBA, select Developer Reference in the drop-down list next to Search and enter one or more terms in the search box.
The following example totals the values from the Freight field for orders shipped to the United Kingdom. The domain is an Orders table. The criteria argument restricts the resulting set of records to those for which ShipCountryRegion equals UK.
Dim curX As Currency
curX = "[Freight]", "Orders", _
"[ShipCountryRegion] = 'UK'"
The next example calculates a total by using two separate criteria. Note that single quotation marks (') and number signs (#) are included in the string expression, so that when the strings are concatenated, the string literal is enclosed in single quotation marks, and the date is enclosed in pound signs.
Dim curX As Currency
curX = "[Freight]", "Orders", _
"[ShipCountryRegion] = 'UK' AND _
[ShippedDate] > #1-1-95#"