Help creating a running sum based on a calculated field in a query (1 Viewer)

Indigo

Registered User.
Local time
Today, 07:00
Joined
Nov 12, 2008
Messages
241
I am running Access 2003 and I am trying to create a running sum based on a calculated field in the query as this query builds a chart.
Here is my SQL prior to adding a DSum expression:

SELECT Left([Activity],12) AS ActivityName,
[CostSavingsperYear]/250000 AS CostReduction,
10.37 AS Target
FROM Activity
GROUP BY Left([Activity],12),
[CostSavingsperYear]/250000, 10.37
ORDER BY [CostSavingsperYear]/250000;

(The Activity field in the Table is a memo field so I am only collecting the first 12 characters as the value on the X-axis for the chart.)

I want to create a running sum of the Cost Reduction field, but am stuck on the DSum expression. I tried:

RunTotal: DSum("CostSavingsperYear/250000","Activity","Left(CostActivity,12) <= '" & Left(A.CostActivity,12) & "'")

but get these results:

ActivityName CostReduction Target RunTotal
ActivityA -------0.96 -------10.37----0.96
ActivityB -------0.96 -------10.37----3.84
ActivityC -------0.96 -------10.37----4.8
ActivityD -------1.92 -------10.37----2.88
ActivityE -------1.92 -------10.37----6.72

When the RunTotal results should be:

0.96
1.92
2.88
4.80
6.72

I read somewhere in trying to find answers that:"In order for a running sum to work correctly in a query, the recordset must have a uniqueID"

So I brought in the ID field from the table and changed the SQL to:

SELECT A.ActivityID, Left([CostActivity],12) AS ActivityName, [CostSavingsperYear]/250000 AS CostReduction, 10.37 AS Target, DSum("CostSavingsperYear/250000","Activity","ActivityID <= " & [A.ActivityID] & "") AS RunTotal
FROM Activity AS A
GROUP BY A.ActivityID, Left([CostActivity],12), [CostSavingsperYear]/250000, 10.37, DSum("CostSavingsperYear/250000","Activity","ActivityID <= " & [A.ActivityID] & "")
ORDER BY [CostSavingsperYear]/250000;

but my RunTotal results are still skewed:

2.88
3.84
4.8
1.92
6.72

Could someone offer some advice / direction to me? Thank you.
 
Last edited:

MStef

Registered User.
Local time
Today, 10:30
Joined
Oct 28, 2004
Messages
2,251
Look at "DemoRunningTotalA2000.mdb" (attachment, zip).
Look at form, query (column RunTotal), Module1.
Open form and try.
 

Attachments

  • DemoRunningTotalA2000.zip
    14.4 KB · Views: 302

Indigo

Registered User.
Local time
Today, 07:00
Joined
Nov 12, 2008
Messages
241
Oh that is fantastic! Thank you so much. I had been struggling with the DSum for a couple of days and just couldn't get it to work properly.
 

Indigo

Registered User.
Local time
Today, 07:00
Joined
Nov 12, 2008
Messages
241
Um, here is a glitch I wasn't expecting.....why is it that everytime the query is run, the RunSum increases the value exponentially?
 

Indigo

Registered User.
Local time
Today, 07:00
Joined
Nov 12, 2008
Messages
241
oops, A2 = 0 on the form..... caught your other post on October 31
 

Users who are viewing this thread

Top Bottom