Option Compare Database
Option Explicit
Public Function fnRunTotal(QueryName As String, FieldNameToSum As String, FiscalMonth As Long, CropYear As Long) As Double
Dim rs As DAO.Recordset
With CurrentDb.OpenRecordset( _
"select [" & FieldNameToSum & "] as expr1, [fiscalmonth] from [" & QueryName & "] " & _
"where [cropyr] = " & CropYear & " order by [cropyr], [fiscalmonth];")
If Not (.BOF And .EOF) Then
.FindFirst "[fiscalmonth] = " & FiscalMonth
If Not .NoMatch Then
fnRunTotal = fnRunTotal + !expr1
.MovePrevious
While Not .BOF
fnRunTotal = fnRunTotal + !expr1
.MovePrevious
Wend
End If
End If
End With
End Function