Option Compare Text   'string comparisons are not case sensitive
Option Explicit

'modRecordsetFunctions

Function rsCount (rs As Recordset) As Long
    
    Dim Temp As Long
    
    rsCount = 0
    On Error GoTo err_rsCount
    rs.MoveFirst
    rs.MoveLast
    rsCount = rs.RecordCount
    On Error GoTo 0

err_rsCount:
    Exit Function
End Function

Function rsFindMatch (rs As Recordset, critFld As String, critValue As Variant) As Integer
    'critFld is the field to look in for the comparison
    'critValue is the value to compare
    'returns true if a match is found, false on an error, or if there is no match

    Dim fld As Field

    rsFindMatch = False
    On Error GoTo err_rsFindMatch
    Set fld = rs.Fields(critFld)
    rs.MoveFirst
    While Not rs.eof
	If fld.value = critValue Then
	    rsFindMatch = True
	    Exit Function
	Else
	    rs.MoveNext
	End If
    Wend

exit_rsFindMatch:
    Exit Function
err_rsFindMatch:
    Resume exit_rsFindMatch

End Function

Function rsLookup (rs As Recordset, critFld As String, critValue As Variant, retFld As String) As Variant
    'critFld is the field to look in for the comparison
    'critValue is the value to compare
    'retFld is the field to return the value from if a match is found
    'returns value from retFld if a match is found, Null on an error, or if there is no match
    
    Dim fld1 As Field, fld2 As Field
    
    Set fld1 = rs.Fields(critFld)
    Set fld2 = rs.Fields(retFld)
    On Error GoTo err_rsLookup
    rs.MoveFirst
    While Not rs.eof
	If fld1.value = critValue Then
	    rsLookup = fld2.value
	    Exit Function
	Else
	    rs.MoveNext
	End If
    Wend
    
exit_rsLookup:
    rsLookup = Null
    Exit Function
err_rsLookup:
    Resume exit_rsLookup

End Function

Function rsMax (rs As Recordset, fldName As String) As Variant
    'fldName is the field to test
    'returns max value from fldName if a match is found, Null on an error
    
    Dim fld As Field
    Dim Temp As Variant
    
    Set fld = rs.Fields(fldName)
    On Error GoTo err_rsMax
    rs.MoveFirst
    Temp = fld.value
    rs.MoveNext
    While Not rs.eof
	If (fld.value > Temp) Or IsNull(Temp) Then
	    Temp = fld.value
	End If
	rs.MoveNext
    Wend
    rsMax = Temp

exit_rsMax:
    Exit Function
err_rsMax:
    rsMax = Null
    Resume exit_rsMax
    
End Function

Function rsMin (rs As Recordset, fldName As String) As Variant
    'fldName is the field to test
    'returns min value from fldName if a match is found, Null on an error
    
    Dim fld As Field
    Dim Temp As Variant
    
    Set fld = rs.Fields(fldName)
    On Error GoTo err_rsMin
    rs.MoveFirst
    Temp = fld.value
    rs.MoveNext
    While Not rs.eof
	If fld < Temp Then Temp = fld.value
	rs.MoveNext
    Wend
    rsMin = Temp
    
exit_rsMin:
    Exit Function
err_rsMin:
    rsMin = Null
    Resume exit_rsMin
    
End Function

Function rsSum (rs As Recordset, fldName As String) As Variant
    'fldName is the field to test
    'returns sum of all non-null values from fldName if a match is found, Null on an error
    'null values as treated as zeros

    Dim fld As Field
    Dim Temp As Variant
    
    Set fld = rs.Fields(fldName)
    On Error GoTo err_rsSum
    rs.MoveFirst
    If IsNull(fld.value) Then
	Temp = 0
    Else
	Temp = fld.value
    End If
    rs.MoveNext
    While Not rs.eof
	If IsNull(fld.value) Then
	    Temp = Temp
	Else
	    Temp = Temp + fld.value
	End If
	rs.MoveNext
    Wend
    rsSum = Temp

exit_rsSum:
    Exit Function
err_rsSum:
    rsSum = Null
    Resume exit_rsSum

End Function

