I
info
Hello to All,
I'm re-using some code from MS kb-143237, which calcs a moving average
using seek. The code works great provided that there is data for all
the periods between the week end date and the earliest week end date.
However, when there isn't data for a particular week it errors out "no
current record". What I'd like, is to have zero added to the sum and
the i value incremented, so that it will move onto the next week and
continue averaging. I've tried using a MyRst.NoMatch but can't seem to
figure out where to add it and how to increment the i value. Can
someone suggest something please?
Here's the code:
Function MAvgs(Periods As Integer, WeekEndDate, EarliestDate)
Dim MyDB As Database
Dim MyRst As DAO.Recordset
Dim MySum As Double
Dim i, x
Set MyDB = CurrentDb()
Set MyRst = MyDB.OpenRecordset("tblLabor")
MyRst.Index = "PrimaryKey" 'WeeksSundayDate
x = Periods - 1
ReDim Store(x)
MySum = 0
For i = 0 To x
MyRst.MoveFirst
MyRst.Seek "=", WeekEndDate
Store(i) = MyRst![VarAmt]
If i <> x Then WeekEndDate = WeekEndDate - 7
If WeekEndDate < EarliestDate Then MAvgs = Null: Exit Function
MySum = Store(i) + MySum
Next i
MAvgs = MySum / Periods
MyRst.Close
Set MyDB = Nothing
Set MyRst = Nothing
End Function
tia
Mo
I'm re-using some code from MS kb-143237, which calcs a moving average
using seek. The code works great provided that there is data for all
the periods between the week end date and the earliest week end date.
However, when there isn't data for a particular week it errors out "no
current record". What I'd like, is to have zero added to the sum and
the i value incremented, so that it will move onto the next week and
continue averaging. I've tried using a MyRst.NoMatch but can't seem to
figure out where to add it and how to increment the i value. Can
someone suggest something please?
Here's the code:
Function MAvgs(Periods As Integer, WeekEndDate, EarliestDate)
Dim MyDB As Database
Dim MyRst As DAO.Recordset
Dim MySum As Double
Dim i, x
Set MyDB = CurrentDb()
Set MyRst = MyDB.OpenRecordset("tblLabor")
MyRst.Index = "PrimaryKey" 'WeeksSundayDate
x = Periods - 1
ReDim Store(x)
MySum = 0
For i = 0 To x
MyRst.MoveFirst
MyRst.Seek "=", WeekEndDate
Store(i) = MyRst![VarAmt]
If i <> x Then WeekEndDate = WeekEndDate - 7
If WeekEndDate < EarliestDate Then MAvgs = Null: Exit Function
MySum = Store(i) + MySum
Next i
MAvgs = MySum / Periods
MyRst.Close
Set MyDB = Nothing
Set MyRst = Nothing
End Function
tia
Mo