Results 1 to 14 of 14
  1. #1
    maxmaggot is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Location
    Ireland
    Posts
    91

    Cumulative total error in query

    I've been trying to get a cumulative total by month for expenses for the last few days and I just can't get it to work. Here is the code for the query



    Code:
     
    
    SELECT DISTINCTROW Format(DatePart("m",[InvoiceDate]),"00") & " - " & Format(DatePart("yyyy",[InvoiceDate]),"0000") AS Expr1, Expenses.[Expense Type], Sum(Expenses.Amount) AS [Sum Of Amount], (DSum("Amount","Expenses","[Expenses].[Expense Type] <=" & [Expenses].[Expense Type])) AS [Running Total]
    FROM Expenses
    GROUP BY Format(DatePart("m",[InvoiceDate]),"00") & " - " & Format(DatePart("yyyy",[InvoiceDate]),"0000"), Expenses.[Expense Type]
    HAVING (((Format(DatePart("m",[InvoiceDate]),"00") & " - " & Format(DatePart("yyyy",[InvoiceDate]),"0000")) Not Like "?-?"))
    ORDER BY Format(DatePart("m",[InvoiceDate]),"00") & " - " & Format(DatePart("yyyy",[InvoiceDate]),"0000"), Expenses.[Expense Type];
    It's this line that won't give in
    Code:
    (DSum("Amount","Expenses","[Expenses].[Expense Type] <=" & [Expenses].[Expense Type])) AS [Running Total]
    Anyone able to help?

    I can PM someone the db if necessary
    Last edited by maxmaggot; 06-22-2013 at 10:55 AM.

  2. #2
    rzw0wr is offline I will always be a newbie
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Indiana
    Posts
    479
    What data type is Expense Type?
    If it is text then your quotes are wrong.
    You are showing it as a number.

    Dale

  3. #3
    maxmaggot is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Location
    Ireland
    Posts
    91
    The database is going up on a Sharepoint server so I have tried to limit the number of relationships. The data type for Expense type is a lookup field. i would love to move it out to its own table and link it but I get web compatibility issues when I do that. Hope that helps.

    I changed quotes to the following
    Code:
    (DSum("Amount","Expenses",[Expenses].[Expense Type] <= [Expenses].[Expense Type])) AS [Running Total]
    I don't get an error but it gives me just insane values.

    Could I PM you the db if this doesn't work. My boss is nailing me to the wall to get this working?
    Last edited by maxmaggot; 06-22-2013 at 11:20 AM. Reason: code edit

  4. #4
    maxmaggot is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Location
    Ireland
    Posts
    91
    Ok I fixed quotes and get values now but they are totally of the scale. Any ideas anyone
    New Code
    Code:
    SELECT DISTINCTROW Format(DatePart("m",[InvoiceDate]),"00") & " - " & Format(DatePart("yyyy",[InvoiceDate]),"0000") AS Expr1, Expenses.[Expense Type], Sum(Expenses.Amount) AS [Sum Of Amount], Sum((DSum("Amount","Expenses",[Expense Type]<=[Expenses].[Expense Type]))) AS [Running Total]
    FROM Expenses
    GROUP BY Format(DatePart("m",[InvoiceDate]),"00") & " - " & Format(DatePart("yyyy",[InvoiceDate]),"0000"), Expenses.[Expense Type]
    HAVING (((Format(DatePart("m",[InvoiceDate]),"00") & " - " & Format(DatePart("yyyy",[InvoiceDate]),"0000")) Not Like "?-?"))
    ORDER BY Format(DatePart("m",[InvoiceDate]),"00") & " - " & Format(DatePart("yyyy",[InvoiceDate]),"0000"), Expenses.[Expense Type];
    cumulative line
    Code:
    Sum((DSum("Amount","Expenses",[Expense Type]<=[Expenses].[Expense Type]))) AS [Running Total]
    Any help?

  5. #5
    rzw0wr is offline I will always be a newbie
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Indiana
    Posts
    479
    leave the sum() off the Dsum()
    then try it witout the where clause.

    Dale

  6. #6
    maxmaggot is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Location
    Ireland
    Posts
    91
    ok changed code to
    Code:
    SELECT DISTINCTROW Format(DatePart("m",[InvoiceDate]),"00") & " - " & Format(DatePart("yyyy",[InvoiceDate]),"0000") AS Expr1, Expenses.[Expense Type], Sum(Expenses.Amount) AS [Sum Of Amount], (DSum("Amount","Expenses",[Expense Type]<=[Expenses].[Expense Type])) AS RunningTotal
    FROM Expenses
    GROUP BY Format(DatePart("m",[InvoiceDate]),"00") & " - " & Format(DatePart("yyyy",[InvoiceDate]),"0000"), Expenses.[Expense Type]
    HAVING (((Format(DatePart("m",[InvoiceDate]),"00") & " - " & Format(DatePart("yyyy",[InvoiceDate]),"0000")) Not Like "?-?"))
    ORDER BY Format(DatePart("m",[InvoiceDate]),"00") & " - " & Format(DatePart("yyyy",[InvoiceDate]),"0000"), Expenses.[Expense Type];
    Problem line
    Code:
    (DSum("Amount","Expenses",[Expense Type]<=[Expenses].[Expense Type])) AS RunningTotal
    Getting results but still insane values.

    Sorry for the hassle.

  7. #7
    rzw0wr is offline I will always be a newbie
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Indiana
    Posts
    479
    I thought you would. The problem appears to be in you where clause.

    Not knowing what data the expense type holds as far a values. I can't offer any advice.

    I know expense type is text. you are trying to filter less than or equals to text. This will not work.
    Even if the the text is a number symbol "6" does not = 6.


    Dale

  8. #8
    rzw0wr is offline I will always be a newbie
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Indiana
    Posts
    479
    Max, This what you looking for?

    or are you looking for a grand total of ALL the expenses?




    Click image for larger version. 

Name:	Max.PNG 
Views:	14 
Size:	36.1 KB 
ID:	12845

    Dale

  9. #9
    maxmaggot is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Location
    Ireland
    Posts
    91
    That is what I am seeing

    In the Query - Expenses Query Main it sums the total for each expense by month. What the customer wants is another field "RunningTotal" that would, for instance, show the total for each expense in month 2 + month 1 and so forth e.g. month 3 would show the Running total for month 1+2+3. Hope that helps. I'm generating a report from this query and filtering it by month.

  10. #10
    rzw0wr is offline I will always be a newbie
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Indiana
    Posts
    479
    I am thinking what you want to do could better be done in a report or maybe a form.

    Then you could use VBA a lots more commands.


    Dale

  11. #11
    maxmaggot is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Location
    Ireland
    Posts
    91
    Yeah, I'm thinking that myself at this stage. Thanks for the help. I'll start coding now. At least I can close down this path of trying to do ti.

    Again thanks for all your help.
    Karl

  12. #12
    rzw0wr is offline I will always be a newbie
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Indiana
    Posts
    479
    You have a PM.
    Dale

  13. #13
    maxmaggot is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2013
    Location
    Ireland
    Posts
    91
    Thanks for your help today. Didn't get a solution but I tried a cross-tab query for the first time and while the result isn't pretty in report view it is able to accumulate totals with alot of extra work

    Hope that helps someone else if they are looking for help on this.

    Again not a pretty solution and most likely not ideal but something that worked for me (hopefully)

    Thank you

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,951
    Report would probably be best output vehicle for this grouping and summarizing. Textboxes on report have a RunningSum property.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Using dsum to return a cumulative total
    By tomnsd in forum Queries
    Replies: 7
    Last Post: 04-01-2013, 01:16 PM
  2. Running Total (Cumulative)
    By jamesborne in forum Queries
    Replies: 3
    Last Post: 12-26-2011, 09:30 PM
  3. cumulative total
    By afshin in forum Queries
    Replies: 14
    Last Post: 08-11-2011, 01:42 AM
  4. Cumulative total in query
    By MikeWaring in forum Queries
    Replies: 2
    Last Post: 12-18-2010, 01:40 PM
  5. Replies: 2
    Last Post: 09-30-2009, 09:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums