Function RunAvgQ(QueryName As String, KeyName As String, KeyValue, _
FieldNameToGet As String, RunLength As Integer) As Double
'*************************************************************
' FUNCTION: RunAvgQ()
' Note: Much of this code is from the PrevRecVal function from the Microsoft KB.
' PURPOSE: Calculate a running average within a saved, non-dynamic query,
' based on a given field and number of records to work back through.
' PARAMETERS:
' QueryName - The name of the saved query with the running average.
' KeyName - The name of the query's unique key field.
' KeyValue - The current record's key value.
' FieldNameToGet - The name of the field in the previous
' record from which to retrieve the values.
' RunLength - The length of the running average
' RETURNS: The value in the field FieldNameToGet from the
' previous query record.
' EXAMPLE:
' =RunAvgQ("qryRunAvgMileage","ID",[ID],"OdometerReading",4)
'**************************************************************
Dim RS As Recordset
Dim db As Database
Dim qdef As QueryDef
Dim i As Integer, CalcAvg As Single, strSQL As String, DataLineID As Integer
Dim SampWt As Single, Wt As Single
On Error GoTo Err_RunAvgQ
Set db = CurrentDb()
' Grab the SQL for the query, so we can create a duplicate recordset
Set qdef = db.QueryDefs(QueryName)
strSQL = RemoveFormReference(qdef.SQL)
[B][COLOR="Red"]--DEBUG PRINT WORKS UP TO HERE[/COLOR][/B]
' Open a duplicate recordset to that of the query calling this function
Set RS = db.OpenRecordset(strSQL, dbOpenDynaset)
' The following original line of code doesn't work if you reference a form to filter to
' a certain set of records (which I do).
' It responds with a wildly misleading error that says you have used too few arguments.
' Set RS = db.OpenRecordset("Run Avg rpt query", dbOpenDynaset)
' Find the current record.
Select Case RS.Fields(KeyName).Type
' Find using numeric data type key value?
Case DB_INTEGER, DB_LONG, DB_CURRENCY, DB_SINGLE, DB_DOUBLE, DB_BYTE
RS.FindFirst "[" & KeyName & "] = " & KeyValue
' Find using date data type key value?
Case DB_DATE
RS.FindFirst "[" & KeyName & "] = #" & KeyValue & "#"
' Find using text data type key value?
Case DB_TEXT
RS.FindFirst "[" & KeyName & "] = '" & KeyValue & "'"
Case Else
MsgBox "ERROR: Invalid key field data type!"
Exit Function
End Select
' Running Average code. The On Error will cause the returning value to be
' blank if the current record is lower than the length of the running average.
' First, check to see if the query has at least enough records to do a
' running average.
If RS.RecordCount > RunLength Then
CalcAvg = 0
For i = 1 To RunLength
' Add the previous values
CalcAvg = CalcAvg + Val(RS(FieldNameToGet))
' Move to the previous record.
RS.MovePrevious
Next i
' Divide by the run length for the average of the accumulated values
CalcAvg = CalcAvg / RunLength
' Return the result.
RunAvgQ = CalcAvg
End If
RS.Close
Bye_RunAvgQ:
Exit Function
Err_RunAvgQ:
' This function is designed to return a blank value for any record number
' lower than the length of the running average. So, for a running average
' of 4, the first three records in the query will have a blank for the
' running average.
RunAvgQ = "0"
Resume Bye_RunAvgQ
End Function