Is there an easy way to get the minimum, maximum and average value of a recordset.
I currently have a recordset that works on an union query and that is filtered for dates and selection query. I then take two values put them in a function to create a number. I wish to return the average, the minimum and maximum, and the number of records.
I am doing this but very clumsily. Comments on teh code and my methods would be appreciated.
Private Function IssuedReceived()
Dim strsite As String
Dim db As Database, rs As Recordset, varTestProgram As Variant, lngPrevOrderID
Dim booStatus As Boolean, intFlagCount As Integer
Dim intAge As Integer
Dim intCountAge As Integer
Dim intCountRecords As Integer
Dim intmax As Integer
Dim intmin As Integer
Dim DtStart As Date
Dim DtEnd As Date
Dim strSQL As String
intmin = 0
intmax = 0
intAge = 0
DtStart = Me.BeginDate
DtEnd = Me.EndDate
If Me.Site <> "Both Sites" Then
If Me.Site = "Allston" Then
strsite = "A"
ElseIf Me.Site = "Framingham" Then
strsite = "F"
End If
strSQL = "SELECT DateIssued, Origin, DCRReceivedDate" _
& " FROM qryUnionMOVA WHERE qryUnionMOVA.Origin = '" & strsite & "' AND " & " qryUnionMOVA.DateIssued Between #" & DtStart & "# And #" & DtEnd & "#"
ElseIf Me.Site = "Both Sites" Then
strSQL = "SELECT DateIssued, Origin, DCRReceivedDate" _
& " FROM qryUnionMOVA WHERE qryUnionMOVA.DateIssued Between #" & DtStart & "# And #" & DtEnd & "#"
End If
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
rs.MoveFirst
intAge = OpenWorkDays(rs!DCRReceivedDate, rs!DateIssued)
intmin = intAge
rs.MoveFirst
Do Until rs.EOF
intAge = OpenWorkDays(rs!DCRReceivedDate, rs!DateIssued)
'intMin = intAge
intCountAge = intCountAge + intAge
Select Case intAge
Case Is > intmax
intmax = intAge
Case Is < intmin
intmin = intAge
End Select
rs.MoveNext
Loop
intCountRecords = rs.RecordCount
Me.countissued = intCountRecords
Me.averageissued = intCountAge / intCountRecords
Me.Minissued = intmin
Me.maxissued = intmax
End Function
I currently have a recordset that works on an union query and that is filtered for dates and selection query. I then take two values put them in a function to create a number. I wish to return the average, the minimum and maximum, and the number of records.
I am doing this but very clumsily. Comments on teh code and my methods would be appreciated.
Private Function IssuedReceived()
Dim strsite As String
Dim db As Database, rs As Recordset, varTestProgram As Variant, lngPrevOrderID
Dim booStatus As Boolean, intFlagCount As Integer
Dim intAge As Integer
Dim intCountAge As Integer
Dim intCountRecords As Integer
Dim intmax As Integer
Dim intmin As Integer
Dim DtStart As Date
Dim DtEnd As Date
Dim strSQL As String
intmin = 0
intmax = 0
intAge = 0
DtStart = Me.BeginDate
DtEnd = Me.EndDate
If Me.Site <> "Both Sites" Then
If Me.Site = "Allston" Then
strsite = "A"
ElseIf Me.Site = "Framingham" Then
strsite = "F"
End If
strSQL = "SELECT DateIssued, Origin, DCRReceivedDate" _
& " FROM qryUnionMOVA WHERE qryUnionMOVA.Origin = '" & strsite & "' AND " & " qryUnionMOVA.DateIssued Between #" & DtStart & "# And #" & DtEnd & "#"
ElseIf Me.Site = "Both Sites" Then
strSQL = "SELECT DateIssued, Origin, DCRReceivedDate" _
& " FROM qryUnionMOVA WHERE qryUnionMOVA.DateIssued Between #" & DtStart & "# And #" & DtEnd & "#"
End If
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
rs.MoveFirst
intAge = OpenWorkDays(rs!DCRReceivedDate, rs!DateIssued)
intmin = intAge
rs.MoveFirst
Do Until rs.EOF
intAge = OpenWorkDays(rs!DCRReceivedDate, rs!DateIssued)
'intMin = intAge
intCountAge = intCountAge + intAge
Select Case intAge
Case Is > intmax
intmax = intAge
Case Is < intmin
intmin = intAge
End Select
rs.MoveNext
Loop
intCountRecords = rs.RecordCount
Me.countissued = intCountRecords
Me.averageissued = intCountAge / intCountRecords
Me.Minissued = intmin
Me.maxissued = intmax
End Function