sh********@yahoo.com wrote:
I'm wondernig if it's possible to do the following:
I'd like to average every 2 numeric values in a table as they appear
sequentially ordered by their time stamps. I was thinking the only way
to do this is to create another column in the table that is populated
with sequential numbers that increase by 1 every two values like so:
1
1
2
2
3
3
And then I can run the average function grouping by this new number
column.
Of course, I cannot figure out how to create this customized autonumber
column.
Any ideas?
Or any ideas for my main goal? How to average every two numeric values
ordered by timestamp?
Thank you!
This routine is a demonstration of how you can get the sequence numbers
you desire. It will display the sequence counts for the first 10
records of a recordset. In your case the SQL might be something like
Dim strSQL As STring
strSQL = "Select * from table1 order by DateTimeStamp"
Set r = currentdb.openrecordset(strSQL,dbopensnapshot)
Create a new code module and insert this code there. If you
misunderstand anything, highlight the word and press F1 for help.
You will need to modify it to suit your needs. Change the table name to
any table you have in the app and run the code.
Sub AP()
Dim r As Recordset
Dim lngMod As Integer 'hold modulus
Dim lngCnt As Long 'do for 10 records
Dim lngPos As Long
'change the table name here to reflect you SQL string/query name.
'You should short the table by date/time stamp when opening the
'recordset.
Set r = CurrentDb.OpenRecordset("Table1", dbOpenSnapshot)
If r.RecordCount > 0 Then
Do While lngCnt < 10
lngCnt = lngCnt + 1 'only display 10 recs
lngPos = r.AbsolutePosition + 1 'start at #1...num recs
lngMod = lngPos Mod 2 'is this odd or even
If lngMod = 0 Then lngMod = 2 'go back 2 if even, 1 if odd
MsgBox "Record Count " & lngCnt & _
" Pos " & lngPos & _
" Mod " & lngMod & _
" AvgCnt " & ((lngPos - lngMod) / 2) + 1
'Note: avgcnt is the absolute position + 1 (start at 1, not 0)
'and subtract that by 1 or 2 to get the last even number.
'Divide the result of the last even number by 2 to get the
'highest count prior to this record and then increment by 1 to
'get the recs avgcnt number
r.MoveNext
If r.EOF Then Exit Do
Loop
end if
r.Close
Set r = Nothing
End Sub