Greetings,
I'm having trouble using the running sum code in a query. It errors out on the code on Select Case rst.Fields(idName).Type and error is ITEM NOT FOUND IN THIS COLLECTION.
My query
SELECT t1.ACCEPTANCE_DT, t1.EXPDTR_AMT, t1.OBJ_CD, t1.TRANS_LINE_NBR, t1.TRANS_NBR, CDbl(RecRunSum("qry_tst_2","2/11/2010",[ACCEPTANCE_DT],"EXPDTR_AMT")) AS Expr2
FROM t1
WHERE (((t1.ACCEPTANCE_DT)>#8/1/2011#) AND ((t1.OBJ_CD)="2000"));
I want to get a running total by acceptance date for transactions greater than 8/1/2011 and object code 2000. I'm using the code in the FAQ. Hopefully, I didn't goof that up.
Public Function RecRunSum(qryName As String, idName As String, idValue, sumField As String)
'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
Dim db As DAO.Database, rst As DAO.Recordset, subSum
Set db = CurrentDb()
Set rst = db.OpenRecordset(qryName, dbOpenDynaset)
'Find the current record via proper syntax for Data Type.
Select Case rst.Fields(idName).Type
Case dbLong, dbInteger, dbCurrency, dbSingle, dbDouble, dbByte
'Numeric
rst.FindFirst "[" & idName & "] = " & idValue
Case dbText
'Text
rst.FindFirst "[" & idName & "] = '" & idValue & "'"
Case dbDate
'Date
rst.FindFirst "[" & idName & "] = #" & idValue & "#"
Case Else
'Unknown data type returns Null
rst.MovePrevious
'Move to BOF
End Select
'Running Sum (subTotal) for each record occurs here.
Do Until rst.BOF
subSum = subSum + Nz(rst(sumField), 0)
rst.MovePrevious
Loop
qryRunSum = subSum
Set rst = Nothing
Set db = Nothing
End Function
I'm having trouble using the running sum code in a query. It errors out on the code on Select Case rst.Fields(idName).Type and error is ITEM NOT FOUND IN THIS COLLECTION.
My query
SELECT t1.ACCEPTANCE_DT, t1.EXPDTR_AMT, t1.OBJ_CD, t1.TRANS_LINE_NBR, t1.TRANS_NBR, CDbl(RecRunSum("qry_tst_2","2/11/2010",[ACCEPTANCE_DT],"EXPDTR_AMT")) AS Expr2
FROM t1
WHERE (((t1.ACCEPTANCE_DT)>#8/1/2011#) AND ((t1.OBJ_CD)="2000"));
I want to get a running total by acceptance date for transactions greater than 8/1/2011 and object code 2000. I'm using the code in the FAQ. Hopefully, I didn't goof that up.
Public Function RecRunSum(qryName As String, idName As String, idValue, sumField As String)
'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
Dim db As DAO.Database, rst As DAO.Recordset, subSum
Set db = CurrentDb()
Set rst = db.OpenRecordset(qryName, dbOpenDynaset)
'Find the current record via proper syntax for Data Type.
Select Case rst.Fields(idName).Type
Case dbLong, dbInteger, dbCurrency, dbSingle, dbDouble, dbByte
'Numeric
rst.FindFirst "[" & idName & "] = " & idValue
Case dbText
'Text
rst.FindFirst "[" & idName & "] = '" & idValue & "'"
Case dbDate
'Date
rst.FindFirst "[" & idName & "] = #" & idValue & "#"
Case Else
'Unknown data type returns Null
rst.MovePrevious
'Move to BOF
End Select
'Running Sum (subTotal) for each record occurs here.
Do Until rst.BOF
subSum = subSum + Nz(rst(sumField), 0)
rst.MovePrevious
Loop
qryRunSum = subSum
Set rst = Nothing
Set db = Nothing
End Function