D
dewpaul
Hello,
I want to calcuate a moving (running) average in Access using VBA. My data
consists of two columns. One column is TIME with every entry being 1 second
and the second is DATA (gas concentration with time) taken every 1 second.
I would like to filter the data using a moving average (say average every 60
seconds). In the end I would like filtered data to be exported as another
table/query with the two columns of filtered/averaged data.
I tried using an example from the Microsoft website and struggled without
success (http://support.microsoft.com/default.aspx?scid=kb;en-us;210138).
With the method below I get an error attibutable to "rst.MoveLast".
Questions: Is this code even a good start? How can I put the data back
into another table after filtering.
Thanks for all input!!
'Function MovAvg(dataSource As Long, period As Integer)
'dataSource is a column of gas concentration data
'period is the period to average over
Dim rst As DAO.Recordset
Dim sql As String
Dim n As Integer
Dim CPCdata As Long
'the expression I use
'Expr1: MovAvg([CPC_3022],60)
sql = "SELECT * FROM (formattedLog) WHERE CPC_3022 = " & dataSource & ""
Set rst = CurrentDb.OpenRecordset(sql)
rst.MoveLast
For n = 0 To period - 1
If rst.BOF Then
MovAvg = 0
Exit Function
Else
ma = ma + rst.Fields("CPC_3022")
End If
rst.MovePrevious
Next n
rst.Close
MovAvg = ma / period
End Function
I want to calcuate a moving (running) average in Access using VBA. My data
consists of two columns. One column is TIME with every entry being 1 second
and the second is DATA (gas concentration with time) taken every 1 second.
I would like to filter the data using a moving average (say average every 60
seconds). In the end I would like filtered data to be exported as another
table/query with the two columns of filtered/averaged data.
I tried using an example from the Microsoft website and struggled without
success (http://support.microsoft.com/default.aspx?scid=kb;en-us;210138).
With the method below I get an error attibutable to "rst.MoveLast".
Questions: Is this code even a good start? How can I put the data back
into another table after filtering.
Thanks for all input!!
'Function MovAvg(dataSource As Long, period As Integer)
'dataSource is a column of gas concentration data
'period is the period to average over
Dim rst As DAO.Recordset
Dim sql As String
Dim n As Integer
Dim CPCdata As Long
'the expression I use
'Expr1: MovAvg([CPC_3022],60)
sql = "SELECT * FROM (formattedLog) WHERE CPC_3022 = " & dataSource & ""
Set rst = CurrentDb.OpenRecordset(sql)
rst.MoveLast
For n = 0 To period - 1
If rst.BOF Then
MovAvg = 0
Exit Function
Else
ma = ma + rst.Fields("CPC_3022")
End If
rst.MovePrevious
Next n
rst.Close
MovAvg = ma / period
End Function