Access query running total

I'm trying to get a running total in my Access 2007 query.  Here's the table:

Sub_Prod_Nam        MonNum         Commission
Tom                           1                          10
Tom                           2                          15
Tom                           3                           5
Dick                           1                          11
Dick                           2                          10
Dick                           3                          20
Harry                         1                           1
Harry                         2                         10
Harry                         3                         10

I want to return this from the query:

Sub_Prod_Num          MonNum       Commission             RunTot
Tom                           1                          10                           10
Tom                           2                          15                            25
Tom                           3                           5                             30
Dick                           1                          11                            11
Dick                           2                          10                            21
Dick                           3                          20                            41
Harry                         1                          1                                1
Harry                         2                         10                             11
Harry                         3                         10                             21


This is what I'm using:

SELECT Sub_Prod_Nam, MonNum, Commission, DSum("[Commission]","[Report 20 mult percentages]","[Sub_Prod_Nam] =" & [Sub_Prod_Nam] & " And [MonNum]<=" & [MonNum]) AS RunTot
FROM [Report 20 mult percentages]
ORDER BY Sub_Prod_Nam, MonNum;

In the RunTot column when I run the query I get #ERROR

What am I doing wrong?

Thanks -
loybobAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeffrey CoachmanMIS LiasonCommented:
Does it have to be in a query...?

If your ultimate goal is to "show" the values with the running total, you can do this easily in a Report.
Juts add the "Commission" field in a second time.
Then on the second "commission" control, set the "running Sum" property to "Over All" (or "Over Group" if applicable)
0
loybobAuthor Commented:
I don't need to show it in a report - I need to feed it into another query that's going to use it.
0
Jeffrey CoachmanMIS LiasonCommented:
Oh, ...ok

I am sure another Expert will be along shortly...
;-)

JeffCoachman
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Jeffrey CoachmanMIS LiasonCommented:
...but you will probably need a Primary key in your tables regardless...
0
mbizupCommented:
It looks close, but you need to delimit text (such as the names) with quotes in your DSum criteria:

SELECT Sub_Prod_Nam, MonNum, Commission, DSum("[Commission]","[Report 20 mult percentages]","[Sub_Prod_Nam] = '" & [Sub_Prod_Nam] & "' And [MonNum]<=" & [MonNum]) AS RunTot
FROM [Report 20 mult percentages]
ORDER BY Sub_Prod_Nam, MonNum;

Open in new window

0
loybobAuthor Commented:
I have a pk - I abbreviated the table when I typed it in - it has many more fields --
0
loybobAuthor Commented:
I put in the single quotes around Sub_Prod_Name, but I still get #Error
0
mbizupCommented:
This is a variation on my last post, which handles single quotes in Irish names such as O'Brien, O'Malley, etc:


SELECT Sub_Prod_Nam, MonNum, Commission, DSum("[Commission]","[Report 20 mult percentages]","[Sub_Prod_Nam] = " & CHR(34) & [Sub_Prod_Nam] & CHR(34) & " And [MonNum]<=" & [MonNum]) AS RunTot
FROM [Report 20 mult percentages]
ORDER BY Sub_Prod_Nam, MonNum;

Open in new window

0
mbizupCommented:
Also, try this - I think you are missing some spaces:

SELECT Sub_Prod_Nam, MonNum, Commission, DSum("[Commission]","[Report 20 mult percentages]","[Sub_Prod_Nam] = " & CHR(34) & [Sub_Prod_Nam] & CHR(34) & " And [MonNum]<= " & [MonNum]) AS RunTot
FROM [Report 20 mult percentages]
ORDER BY Sub_Prod_Nam, MonNum;

Open in new window


If that doesn't help, try checking your DSum on its own in the immediate window of the Visual Basic Environment like this:

MsgBox DSum("[Commission]","[Report 20 mult percentages]","[Sub_Prod_Nam] = " & CHR(34) & "Tom" & CHR(34) & " And [MonNum]<= " & 3) 

Open in new window


Change the criteria where I have entered Tom and "3" to reflect the actual data in your database.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
loybobAuthor Commented:
That did I - I got the DSum working without the field names in the Immediate window, then I put the field variables back in one at a time and it worked.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.