Sum Function Example (DAO)

Microsoft Jet SQL Reference

Sum Function Example

This example uses the Orders table to calculate the total sales for orders shipped to the United Kingdom.

This example calls the EnumFields procedure, which you can find in the SELECT statement example.

Sub SumX()

    Dim dbs As Database, rst As Recordset

    ' Modify this line to include the path to Northwind

    ' on your computer.

    Set dbs = OpenDatabase("Northwind.mdb")

    ' Calculate the total sales for orders shipped to

    ' the United Kingdom. 

    Set rst = dbs.OpenRecordset("SELECT" _

        & " Sum(UnitPrice*Quantity)" _

        & " AS [Total UK Sales] FROM Orders" _

        & " INNER JOIN [Order Details] ON" _

        & " Orders.OrderID = [Order Details].OrderID" _

        & " WHERE (ShipCountry = 'UK');")

    ' Populate the Recordset.

    rst.MoveLast

    ' Call EnumFields to print the contents of the

    ' Recordset. Pass the Recordset object and desired

    ' field width.

    EnumFields rst, 15

    

    dbs.Close

End Sub