Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
[blue]Public Function RecRunSum(qryName As String, idName As String, idValue, sumField As String)
[green]'qryName - Name of the query calling this function
'idName - UniqueFieldName ... usually the primarykey
'idValue - The value of UniqueFieldName in the line above.
'sumField - The name of the field to runsum[/green]
Dim db As DAO.Database, rst As DAO.Recordset, subSum
Set db = CurrentDb()
Set rst = db.OpenRecordset(qryName, dbOpenDynaset)
[green]'Find the current record via proper syntax for Data Type.[/green]
Select Case rst.Fields(idName).Type
Case dbLong, dbInteger, dbCurrency, _
dbSingle, dbDouble, dbByte [green]'Numeric[/green]
rst.FindFirst "[" & idName & "] = " & idValue
Case dbText [green]'Text[/green]
rst.FindFirst "[" & idName & "] = '" & idValue & "'"
Case dbDate [green]'Date[/green]
rst.FindFirst "[" & idName & "] = #" & idValue & "#"
Case Else [green]'Unknown data type returns Null[/green]
rst.MovePrevious 'Move to BOF
End Select
[green]'Running Sum (subTotal) for each record occurs here.[/green]
Do Until rst.BOF
subSum = subSum + Nz(rst(sumField), 0)
rst.MovePrevious
Loop
RecRunSum = subSum
Set rst = Nothing
Set db = Nothing
End Function[/blue]
[tt]Example Data
------------
QueryName = qryInv
UniqueID = InvID
FieldToSum = Cost[/tt]
[blue]Byte ..... [purple][b]RunSum: CByte(RecRunSum("qryInv","InvID",[InvID],"Cost"))[/b][/purple]
Currency . [purple][b]RunSum: CCur(RecRunSum("qryInv","InvID",[InvID],"Cost"))[/b][/purple]
Double ... [purple][b]RunSum: CDbl(RecRunSum("qryInv","InvID",[InvID],"Cost"))[/b][/purple]
Integer .. [purple][b]RunSum: CInt(RecRunSum("qryInv","InvID",[InvID],"Cost"))[/b][/purple]
Long ..... [purple][b]RunSum: CLng(RecRunSum("qryInv","InvID",[InvID],"Cost"))[/b][/purple]
Single ... [purple][b]RunSum: CSng(RecRunSum("qryInv","InvID",[InvID],"Cost"))[/b][/purple][/blue]