totn Access Functions

MS Access: DAvg Function

This MSAccess tutorial explains how to use the Access DAvg function with syntax and examples.

Description

The Microsoft Access DAvg function returns the average of a set of numeric values from an Access table (or domain).

Syntax

The syntax for the DAvg function in MS Access is:

DAvg ( expression, domain, [criteria] )

Parameters or Arguments

expression
The numeric values that you wish to average.
domain
The set of records. This can be a table or a query name.
criteria
Optional. It is the WHERE clause to apply to the domain.

Returns

The DAvg function returns a numeric value.

Applies To

The DAvg function can be used in the following versions of Microsoft Access:

  • Access 2019, Access 2016, Access 2013, Access 2010, Access 2007, Access 2003, Access XP, Access 2000

Example

Let's look at how to use the DAvg function in MS Access:

DAvg("UnitPrice", "Order Details", "OrderID = 10248")

In this example, you would be averaging the UnitPrice field in the Order Details table where the OrderID is 10248. This is the same as the following SQL statement:

SELECT Avg([Order Details].UnitPrice) AS AvgOfUnitPrice
FROM [Order Details]
WHERE ((([Order Details].OrderID)=10248));

You can also average more than one numeric field. For example:

DAvg("UnitPrice * Quantity", "Order Details", "OrderID = 10248")

This example would average 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 Avg([UnitPrice]*[Quantity]) AS Expr1
FROM [Order Details]
WHERE ((([Order Details].OrderID)=10248));

Example in VBA Code

The DAvg function can be used in VBA code in Microsoft Access.

For example:

Dim LTotal As Currency

LTotal = DAvg("UnitPrice", "Order Details", "OrderID = 10248")

In this example, the variable called LTotal would now contain the average UnitPrice from the Order Details table where the OrderID is 10248.

Example in SQL/Queries

You can also use the DAvg function in a query in Microsoft Access.

For example:

Microsoft Access

In this query, we have used the DAvg function as follows:

Expr1: DAvg("UnitPrice","Order Details","OrderID = 10248")

This query will return the OrderID value from the Orders table. It will also return the average UnitPrice from the Order Details table where the OrderID is equal to 10248 (NOTE: The Order Details table contains one record for each item ordered, so there can be more than one record for each OrderID). The results of the DAvg function will be displayed in a column called Expr1. You can replace Expr1 with a column name that is more meaningful.

For example:

AvgPrice: DAvg("UnitPrice","Order Details","OrderID = 10248")

The results would now be displayed in a column called AvgPrice. This example is just used for demonstration purposes to show how to use the DAvg function and what it would return.