Moderate: Requires basic macro, coding, and interoperability skills.
This article applies only to a Microsoft Access database (.mdb).
This article applies only to a Microsoft Access database (.mdb).
Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.
Query: qryGrpRunSum
-------------------
Type: Select Query
Field: CategoryID
Table: Products
Sort: Ascending
Field: UnitsInStock
Table: Products
RunSum: fncRunSum([CategoryID], [UnitsInStock])
Option Compare Database
Option Explicit
Function fncRunSum(lngCatID As Long, lngUnits As Long) As Long
'Variables that retain their values.
Static lngID As Long
Static lngAmt As Long
If lngID <> lngCatID Then
'If the current ID does not match the last ID, then (re)initialize.
lngID = lngCatID
lngAmt = lngUnits
Else
'If the current ID matches the last, keep a running sum for the ID.
lngAmt = lngAmt + lngUnits
End If
'Pass the running sum back to the query.
fncRunSum = lngAmt
End Function
Keywords: kbhowto, kb, kbarchive