Hi
I am trying to create a function to use in a query similar to a running sum. The difference being, instead of adding the field from the record before, I would like access to see whether there is a value in the current record and if there is use that value - if there isn't to then use the value from the previous record. Something like
ID Value result needed
1 100 100
2 0 100
3 150 150
4 0 150
... and so forth
I have tried adapting the if ...then ... else function for fncRunSum but have obviously done something wrong. My adaption is:
Option Compare Database
Option Explicit
Function fncCarryFwd(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 lngUnits = 0 Then
lngAmt = lngAmt
Else
'If the current ID matches the last, keep a running sum for the ID
lngAmt = lngUnits
End If
'Pass the running sum back to the query
fncCarryFwd = lngAmt
End Function
======
Please can one of you experts show me where I am going wrong
thanks
liz
I am trying to create a function to use in a query similar to a running sum. The difference being, instead of adding the field from the record before, I would like access to see whether there is a value in the current record and if there is use that value - if there isn't to then use the value from the previous record. Something like
ID Value result needed
1 100 100
2 0 100
3 150 150
4 0 150
... and so forth
I have tried adapting the if ...then ... else function for fncRunSum but have obviously done something wrong. My adaption is:
Option Compare Database
Option Explicit
Function fncCarryFwd(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 lngUnits = 0 Then
lngAmt = lngAmt
Else
'If the current ID matches the last, keep a running sum for the ID
lngAmt = lngUnits
End If
'Pass the running sum back to the query
fncCarryFwd = lngAmt
End Function
======
Please can one of you experts show me where I am going wrong
thanks
liz