Access: DSum Function
In Access, the DSum function returns the sum of a set of numeric values in a specified set of records (or domain).
The syntax for the DSum function is:
DSum ( expression, domain, [criteria] )
expression is the numeric values that you wish to sum.
domain is the set of records. This can be a table or a query name.
criteria is optional. It is the WHERE clause to apply to the domain.
For example:
Let's take a look at a simple example:
DSum("UnitPrice", "Order Details", "OrderID = 10248")
In this example, you would be summing the UnitPrice field in the Order Details table where the OrderID is 10248. This is the same as the following SQL statement:
SELECT Sum([Order Details].UnitPrice) AS SumOfUnitPrice
FROM [Order Details]
WHERE ((([Order Details].OrderID)=10248));
You can also sum more than one numeric field. For example:
DSum("UnitPrice * Quantity", "Order Details", "OrderID = 10248")
This example would sum the UnitPrice x Quantity for all records in the Order Details table where the OrderID is 10248. This is the same as the following SQL statement:
SELECT Sum([UnitPrice]*[Quantity]) AS Expr1
FROM [Order Details]
WHERE ((([Order Details].OrderID)=10248));
VBA Code
The DSum function can be used in VBA code. For example:
Dim LTotal As Currency
LTotal = DSum("UnitPrice", "Order Details", "OrderID = 10248")
In this example, the variable called LTotal would now contain the summed UnitPrice from the Order Details table where the OrderID is 10248.
SQL/Queries
You can also use the DSum function in a query.
