Access newbie here. I've got a query that pulls a series of dates. I need to calculate the average interval between the dates. Any help would be greatly appreciated.
The following will give you an average in days. You will need to substitute your own SQL string and relevant variable names.
This also assumes that your dates" are actually in date formats. And you need to make a decision as to whether the recordset is going to return the dates in any sorted order. That will profoundly affect the results.
And a last caveat: the code was just written on the fly and is untested.
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim date1 As Date
Dim date2 As Date
Dim lngCt As Long
Dim lngTotal As Long
Set db = CurrentDb
strSQL = "SELECT tblDates.DateID, tblDates.EventDate FROM tblDates WHERE (((tblDates.EventDate) Is Not Null))ORDER BY tblDates.EventDate;"
Set rs = db.OpenRecordset(strSQL)
rs.MoveLast
rs.MoveFirst
If Not rs.BOF And Not rs.EOF Then
date1 = rs!EventDate
rs.MoveNext
While Not rs.EOF
date2 = rs!EventDate
lngTotal = lngTotal + (DateDiff("d", date1, date2))
lngCt = lngCt + 1
date1 = date2
rs.MoveNext
Wend
Duane MS Access MVP
[green]Ask a great question, get a great answer.[/green] [red]Ask a vague question, get a vague answer.[/red]
[green]Find out how to get great answers faq219-2884.[/green]
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.