Hope this can be done
Is there a way to create a Running Sum for a form in Access? I have a field name DIST_STOP which show incremental distances for bus stops. I have a sequential ID field LOC_ID which is unique for each record. Microsoft has advised to do this but I get all zeros in the running sum field.
Open the sample database, Northwind.mdb.
Create the following new query based on the Products table. Query: qryGrpRunSum
-------------------
Type: Select Query
Field: CategoryID
Table: Products
Sort: Ascending
Field: UnitsInStock
Table: Products
In the Field row of the third column in the query design grid, type the following expression:RunSum: fncRunSum([CategoryID], [UnitsInStock])
Save the query as qryGrpRunSum, and then close the query.
Create a new module, and then type or paste the following code: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
Is there a way to create a Running Sum for a form in Access? I have a field name DIST_STOP which show incremental distances for bus stops. I have a sequential ID field LOC_ID which is unique for each record. Microsoft has advised to do this but I get all zeros in the running sum field.
Open the sample database, Northwind.mdb.
Create the following new query based on the Products table. Query: qryGrpRunSum
-------------------
Type: Select Query
Field: CategoryID
Table: Products
Sort: Ascending
Field: UnitsInStock
Table: Products
In the Field row of the third column in the query design grid, type the following expression:RunSum: fncRunSum([CategoryID], [UnitsInStock])
Save the query as qryGrpRunSum, and then close the query.
Create a new module, and then type or paste the following code: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