
Rolling Averages - back to the drawing board
Hi everyone,
Thanks to those who have been trying to solve this problem for me, but
we are not much closer.
I may have to go back to the module idea after all, as the query is
really not producing the rolling average I am looking for, but only
doing some kind of weird averaging thing on the whole data (when I graph
it the result follows the line of the data-not a smoothed average).
This is a module we were playing with here, which we got to work once,
but never again: Perhaps someone can see what might be the problem: At
the moment I get a figure, but it is the same number in every box and
that number is way too high - it is totalling the whole table and
dividing it all by 12.
Function RollingAvgs(Periods As Integer, Month)
Dim MyDB As Database, MyRST As Recordset, MySum As Double
Dim temp As Double
Dim tempmonth As Date
Dim StartMonth As Date
Dim month_count As Integer
Set MyDB = CurrentDb()
Set MyRST = MyDB.OpenRecordset("Table2")
On Error Resume Next
' initialise the sum to zero
MySum = 0
' initialise the month_counter to one
month_count = 0
StartMonth = Month
' check that the month value is within correct range
If StartMonth < #5/1/97# Then RollingAvgs = Null: Exit
Function
' we are going to look at the index "Month"
MyRST.Index = "Month"
' setup the record set to start where we want it to, by
searching for
' the startmonth supplied in the parameter list.
MyRST.MoveFirst
Do Until MyRST.EOF
tempmonth = MyRST![Month]
If tempmonth = StartMonth Then
Exit Do
End If
MyRST.MoveNext
Loop
' we want to loop until we either reach the end of the file,
or have counted
' the desired number of months
Do Until MyRST.EOF Or MyRST.BOF Or month_count >= Periods
temp = MyRST![downtime minutes]
MySum = MySum + temp
' Increment Recordset to look at next record
MyRST.MovePrevious
' decrement counter so that we dont count more than the
period requested
month_count = month_count + 1
Debug.Print month_count
Loop
' now calculate the average using the sum and the number of
months counted
RollingAvgs = MySum / month_count
MyRST.Close
End Function