>
SELECT Sum(UnitPrice * Quantity)You can use the Sum function in a query expression. You can also use this expression in the SQL property of a QueryDef object or when creating a Recordset based on an SQL query. See Also Count Function, SQL Aggregate Functions. Specifics (Microsoft Access) In Microsoft Access, you can use the Sum function in the query design grid, in an SQL statement in SQL view of the Query window, or in an SQL statement within Visual Basic code. You can also use the Sum function in a calculated control on a form or report. The Sum function is most useful in totals queries and crosstab queries. It functions the same way whether you create the query in the query design grid or as an SQL statement in SQL view. In the query design grid, you can create a new totals query by clicking the Totals button on the toolbar. The Total row is then inserted in the grid. You can set the Total cell beneath a field to the aggregate function to perform on the data in that field. For example, suppose you have an Orders table that has both a Freight field and a ShipCity field. You can create a query that displays the sum of freight costs for orders sent to each city. Create a new totals query, and drag the ShipCity field to the query design grid. The Total cell beneath the ShipCity field should be set to Group By. Drag the Freight field to the query design grid, and set the Total cell beneath it to Sum. When you run the query, it will display the sum of freight costs by city. You can view the SQL statement for this query by switching to SQL view. In this example, Microsoft Access creates the following SQL statement.
AS [Total Revenue] FROM [Order Details];
SELECT DISTINCTROW Sum(Orders.Freight) AS SumOfFreight, Orders.ShipCity FROM Orders GROUP BY Orders.ShipCity;You can also use an SQL statement such as this one within Visual Basic code. For example, using the following code you can create a Recordset object based on the query defined by this SQL statement.
Dim dbs As DatabAse, rst As Recordset, strSQL As String Set dbs = CurrentDb strSQL = "SELECT DISTINCTROW Sum(Orders.Freight) AS SumOfFreight, "_ & "Orders.ShipCity FROM Orders GROUP BY Orders.ShipCity;" Set rst = dbs.OpenRecordset(strSQL)To use the Sum function in a calculated control, set the control's ControlSource property to an expression containing the Sum function. For example, to display the sum of freight costs for a set of orders in a text box, enter the following expression in the ControlSource property of the text box.
= Sum([Freight])If you use the Sum function in a calculated control, you can restrict the set of records against which the function is performed by setting the form's Filter property. Example This example uses the Orders table to calculate the total sales for orders shipped to the United Kingdom.
SELECT Sum(UnitPrice*Quantity) AS [Total UK Sales] FROM OrdersExample (Microsoft Access) The following example assumes that you have an Orders table that contains fields called OrderID and ShipCountry, and an OrderDetails table that contain fields called UnitPrice and Quantity. You can use the Sum function to calculate the total sales for orders shipped to the United Kingdom. Enter the following expression in SQL view in the Query window:
INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID
WHERE (ShipCountry = 'UK');
SELECT Sum([UnitPrice]*[Quantity]) AS [TotalUKSales] FROM Orders INNER JOIN [OrderDetails]The next example creates a calculated control that displays the total sales for records in the OrderDetails table. Open a new form and set its RecordSource property to Order Details. Enter the following expression in the ControlSource property of a text box on the form. To apply a condition that limits the search to only some records, such as those for orders shipped to the United Kingdom, set the form's Filter property.
ON Orders.[OrderID] = [OrderDetails].[OrderID] WHERE ([ShipCountry] = 'UK');
= Sum([UnitPrice]*[Quantity])