S
Sashsa Furr
hi
I am doing a running sum calculation in VBA.
It works fine except for one thing - it is rounding the
results!
I want the decimals!
the code is:
Sub CreateRunningSum()
'calculates the runningsum entirely in code by looking at
the last value of the runningsum field
'and adding the 12 month usuage total in $.
Dim r1 As DAO.Recordset
Dim rc1 As Integer
Dim RS As Long
Set r1 = CurrentDb.OpenRecordset("STPOHS_Sort") 'query
that sorts the records according by 12usage$ descending.
r1.MoveFirst
r1.Edit
r1![RunningSum] = r1![12Usage$] 'the first record does not
have a prior value to sum with.
RS = r1![RunningSum]
r1.Update
r1.MoveNext 'move to the second record.
Do
r1.Edit
RS = RS + r1![12Usage$] 'running sum equals the current
value plus the usage value.
r1![RunningSum] = RS 'pass the new runningsum value to the
field.
r1.Update
r1.MoveNext
Loop Until r1.EOF = True
r1.Close
Set r1 = Nothing
End Sub
If i declare LS as an integer, I get an overflow error
which is why I declared it as long.
TIA
Sasha
I am doing a running sum calculation in VBA.
It works fine except for one thing - it is rounding the
results!
I want the decimals!
the code is:
Sub CreateRunningSum()
'calculates the runningsum entirely in code by looking at
the last value of the runningsum field
'and adding the 12 month usuage total in $.
Dim r1 As DAO.Recordset
Dim rc1 As Integer
Dim RS As Long
Set r1 = CurrentDb.OpenRecordset("STPOHS_Sort") 'query
that sorts the records according by 12usage$ descending.
r1.MoveFirst
r1.Edit
r1![RunningSum] = r1![12Usage$] 'the first record does not
have a prior value to sum with.
RS = r1![RunningSum]
r1.Update
r1.MoveNext 'move to the second record.
Do
r1.Edit
RS = RS + r1![12Usage$] 'running sum equals the current
value plus the usage value.
r1![RunningSum] = RS 'pass the new runningsum value to the
field.
r1.Update
r1.MoveNext
Loop Until r1.EOF = True
r1.Close
Set r1 = Nothing
End Sub
If i declare LS as an integer, I get an overflow error
which is why I declared it as long.
TIA
Sasha