Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculate Avg

Status
Not open for further replies.

shelspa

Technical User
Dec 13, 2001
66
US
This should be an easy one but the search didn't find it for me. In a form, I want to calculate the average of all records for one field and put the value into another field (used for control charting). I'm thinking about using the Avg or the DAvg function on "after update" of a field. I don't know the syntax. Ideas?
 
Cut and paste from the help...

AVG Function:

Calculates the arithmetic mean of a set of values contained in a specified field on a query.

Syntax

Avg(expr)

The expr placeholder represents a string expression identifying the field that contains the numeric data you want to average or an expression that performs a calculation using the data in that field. Operands in expr can include the name of a table field, a constant, or a function (which can be either intrinsic or user-defined but not one of the other SQL aggregate functions).

Remarks

The average calculated by Avg is the arithmetic mean (the sum of the values divided by the number of values). You could use Avg, for example, to calculate average freight cost.

The Avg function doesn't include any Null fields in the calculation.

You can use Avg in a query expression and in the SQL property of a QueryDef object or when creating a Recordset object based on an SQL query.


DAVG:

You can use the DAvg function to calculate the average of a set of values in a specified set of records (a domain). Use the DAvg function in Visual Basic code or in a macro, in a query expression, or in a calculated control.
For example, you could use the DAvg function in the criteria row of a select query on freight cost to restrict the results to those records where the freight cost exceeds the average. Or you could use an expression including the DAvg function in a calculated control, and display the average value of previous orders next to the value of a new order.

Syntax

DAvg(expr, domain[, criteria])

The DAvg function has the following arguments.

Argument Description
expr An expression that identifies the field containing the numeric data you want to average. It can be a string expression identifying a field in a table or query, or it can be an expression that performs a calculation on data in that field. In expr, you can include the name of a field in a table, a control on a form, a constant, or a function. If expr includes a function, it can be either built-in or user-defined, but not another domain aggregate or SQL aggregate function.
domain A string expression identifying the set of records that constitutes the domain. It can be a table name or a query name.
criteria An optional string expression used to restrict the range of data on which the DAvg function is performed. For example, criteria is often equivalent to the WHERE clause in an SQL expression, without the word WHERE. If criteria is omitted, the DAvg function evaluates expr against the entire domain. Any field that is included in criteria must also be a field in domain; otherwise the DAvg function returns a Null.
Remarks

Records containing Null values aren't included in the calculation of the average.
Whether you use the DAvg function in a macro or module, a query expression, or a calculated control, you must construct the criteria argument carefully to ensure that it will be evaluated correctly.
You can use the DAvg function to specify criteria in the Criteria row of a query. For example, suppose you want to view a list of all products ordered in quantities above the average order quantity. You could create a query on the Orders, Order Details, and Products tables, and include the Product Name field and the Quantity field, with the following expression in the Criteria row beneath the Quantity field:

>DAvg("[Quantity]", "Orders")

You can also use the DAvg function within a calculated field expression in a query, or in the Update To row of an update query.

Note You can use either the DAvg or Avg function in a calculated field expression in a totals query. If you use the DAvg function, values are averaged before the data is grouped. If you use the Avg function, the data is grouped before values in the field expression are averaged.

Use the DAvg function in a calculated control when you need to specify criteria to restrict the range of data on which the DAvg function is performed. For example, to display the average cost of freight for shipments sent to California, set the ControlSource property of a text box to the following expression:

=DAvg("[Freight]", "Orders", "[ShipRegion] = 'CA'")

If you simply want to average all records in domain, use the Avg function.
You can use the DAvg function in a module or macro or in a calculated control on a form if a field that you need to display isn't in the record source on which your form is based. For example, suppose you have a form based on the Orders table, and you want to include the Quantity field from the Order Details table in order to display the average number of items ordered by a particular customer. You can use the DAvg function to perform this calculation and display the data on your form.

Tips

· If you use the DAvg function in a calculated control, you may want to place the control on the form header or footer so that the value for this control is not recalculated each time you move to a new record.
· If the data type of the field from which expr is derived is a number, the DAvg function returns a Double data type. If you use the DAvg function in a calculated control, include a data type conversion function in the expression to improve performance.

· Although you can use the DAvg function to determine the average of values in a field in a foreign table, it may be more efficient to create a query that contains all of the fields that you need, and then base your form or report on that query.

Note Unsaved changes to records in domain aren't included when you use this function. If you want the DAvg function to be based on the changed values, you must first save the changes by clicking Save Record on the File menu, moving the focus to another record, or by using the Update method.

Hope that helps...
Terry
**************************
* General Disclaimor - Please read *
**************************
Please make sure your post is in the CORRECT forum, has a descriptive title, gives as much detail to the problem as possible, and has examples of expected results. This will enable me and others to help you faster...
 
In the After_Update event of the field add this:

txtAvg=AVG([Name of Field])

If it is on a report or something that doesn't need to be recalculated constantly, you can also put the following in the Control Source property on the Data Tab:

=Avg([Name of Field])

For more information, check the Help Topic "Calculated Controls" or "calculated fields" _________
Rott Paws
 
This is great info. However, I need something more background. Under what field's "after_update" event does "txtAvg=AVG([Name of Field])" go? Also is txtAvg a field?
 
txtAvg is an example name of a text box control. Substitute txtAvg with the name of your control in the code.

Place it in the After_Update event of the field that you want averaged.

For example, say you have a field called UnitsShipped and that value is displayed in a text box called txtUnitsShipped. You have another text box called txtAvgShipped that you want to display the average number of units shipped in all records. Every time a value is entered or changed in a record, you want the average to recalculate.

Go to the properties event properties for txtUnitsShipped and enter "[Event Procedure]" in the After Update event, then click the build button (the button that has "...") to go to the event in the code module.

Your event should look like this:

Private Sub txtUnitsShipped_AfterUpdate()

txtAvgShipped=Avg([UnitsShipped])

End Sub
_________
Rott Paws
 
That's probably because you have it as an unbound control and it doesn't know what to display in it.

In the example above, if you change or enter a value in the txtUnitsShipped text box, it should then display the average.

If that works, then goto the data tab of properties for txtAvgShipped and enter the following into the Control Source property:
=Avg([UnitsShipped])
That will populate it with a value when the form opens, and then the afterupdate event will recalculate it whenever changes are made. _________
Rott Paws
 
It is not displaying the Average. I get "sub or function not defined."
 
OK. Let's retreat, regroup, and attack it from a different angle.

If you already set the Control Source for the control that will display the average as described above, it should work when you open the form. If not, set it as follows:
=Avg([FieldName])

One thing I forgot to consider yesterday is data in the underlying table isn't updated until the record is saved. So you'll have to save the record before recalculating the average field or it does no good.

In the AfterUpdate event for the field to be averaged, you'll first have to save the record and then requery the control that displays the average:

'Save record
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

'Recalculate the average
txtAvgShipped.Requery


I apologize for making this so complicated.
_________
Rott Paws
 
This is great, I'm actually getting the number on the form. It will not show up in the table though. The SaveRecord - requery code does not seem to work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top