Avg Function Example (DAO)

Microsoft Jet SQL Reference

Avg Function Example

This example uses the Orders table to calculate the average freight charges for orders with freight charges over $100.

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

Sub AvgX()

    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 average freight charges for orders

    ' with freight charges over $100. 

    Set rst = dbs.OpenRecordset("SELECT Avg(Freight)" _

        & " AS [Average Freight]" _

        & " FROM Orders WHERE Freight > 100;")

    

    ' Populate the Recordset.

    rst.MoveLast

    

    ' Call EnumFields to print the contents of the

    ' Recordset. Pass the Recordset object and desired

    ' field width.

    EnumFields rst, 25

    dbs.Close

End Sub