Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Running Total Query 2

Status
Not open for further replies.

JOEYB99

Technical User
Jul 9, 2008
121
CA
I am using Access 2007 and having trouble getting a running total in a query.

Here is the basic data, coming from a table called tblLabourHours2

ID (autonumber generated)
Formatted Cost Code
TransDate
Hours

My goal is to get a running total of hours for each cost code. A cost code may have hours for several different dates.

I need to know the running total for each date within a cost code since I may run reports/queries based on a date range.



The columns of my query has ID, Formatted Cost Code, TransDate, Hours, and the following expression in the next column

Expr1: DSum("Total Hours","tblLabourHours2","Formatted Cost Code = " & [Formatted Cost Code] & " And ID <=" & [ID])


I get #ERROR in the expression column.

What am I doing wrong?
 
What about this ?
DSum("Hours","tblLabourHours2","[Formatted Cost Code]='" & [Formatted Cost Code] & "' And ID<=" & [ID])

If [Formatted Cost Code] is defined as numeric then get rid of the single quotes.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you for the prompt reply PHV but it did not work, if I applied your single quotes properly.

Please confirm. You have a single quote just to the right of the first equal sign, and then you have the second single quote just to the left of the And. Correct?

The cost code is a text field.
 
Please, post the SQL code of your query.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Okay then PHV, how's this?

SELECT tblLabourHours2.ID, tblLabourHours2.[Formatted Cost Code], tblLabourHours2.[TransDate], tblLabourHours2.[Total Hours], DSum("Total Hours","tblLabourHours2","Formatted Cost Code =" & [Formatted Cost Code] & "And ID <=" & [ID]) AS Expr1
FROM tblLabourHours2;
 
So, try this:
SQL:
SELECT ID, [Formatted Cost Code], TransDate, [Total Hours]
, DSum("[Total Hours]","tblLabourHours2","[Formatted Cost Code]='" & [Formatted Cost Code] & "' AND ID<=" & [ID]) AS RunningTotalHours
FROM tblLabourHours2

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you PHV. It is still not working but I think we are close.

When I click on the #ERROR result I get the following message,

Syntax error (missing operator) in query expression 'Formatted Cost Code = 'LAB.123' AND ID <= 4'.

It appears to be something do with the proper use of single/double quotes.

The ID field is a numeric field and the cost code field is set up as text.

 
Do:[tt]

Dim strSQL As String
strSQL = "Select ..."[blue]
Debug.Print strSQL[/blue]
[/tt]

and show us what you have in the Immediate Window.
This way (probably) you will see what's wrong with it....

Have fun.

---- Andy
 
Okay Andy, thank you for the suggestion but I'm not familiar with that code. Is that VBA or SQL?

Where exactly do I input it?
 
Again,
SQL:
SELECT ID, [Formatted Cost Code], TransDate, [Total Hours]
, DSum("[!][[/!]Total Hours[!]][/!]","tblLabourHours2","[!][[/!]Formatted Cost Code[!]][/!]='" & [Formatted Cost Code] & "' AND ID<=" & [ID]) AS RunningTotalHours
FROM tblLabourHours2

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV, but can you please clarify.

I see the red square brackets. Should I remove them?
 

Eureka, it now works! Thank you PHV. You are correct about the exact placement of the square brackets.

I also appreciate the input from Andy. I was not familiar with that debugging technique. I did not use it but plan to use it in the future.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top