Results 1 to 6 of 6
  1. #1
    Dave D is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jun 2014
    Posts
    45

    DSUM with a Group By

    I have a "working" DSUM that populates a text box on a report. However, it is picking up duplicates and I need to sum only unique rows. I generated a query that will give me what I need, but I can't figure out how to re-write it into a DSUM, incorporating the Group By, so I can throw it into the text box's Control Source.



    Any help is much appreciated.


    This is the working DSUM (that sums duplicate rows):
    Code:
    =Nz(DSum("invValue","tblTrackingDelivery","cRecordId="+CStr([cRecordId])+" AND Month(vPayNLTDate) = 11"),0)
    This is the query that will give me what I need:
    Code:
    SELECT tblTrackingDelivery.cRecordId, tblTrackingDelivery.invValue, Month([vPayNLTDate])=11 AS November
    FROM tblTrackingDelivery
    GROUP BY tblTrackingDelivery.cRecordId, tblTrackingDelivery.invValue, Month([vPayNLTDate])=11
    HAVING (((Month([vPayNLTDate])=11)=True));

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Use the name of the query object in place of the table name. The query is already filtered by month so that criteria is not needed in the DSum. Why are you use CStr? If cRecordID is a number type field, this would not work.

    =Nz(DSum("invValue","queryname", "cRecordId=" & CStr([cRecordId])),0)
    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.

  3. #3
    Dave D is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jun 2014
    Posts
    45
    I'm trying to display monthly totals in 12 different text boxes; the DSUM example was for the month of November. I provided the query example to show how a query would get the same information for the same month. I was hoping someone could look at the DSUM and the query and tell me how to put the two together in another DSUM - again, this is assuming DSUM can use Group By.

    As for why use CStr([cRecordId]), it's because you need it when you have additional WHERE clauses behind it. If I didn't have the + " AND Month(vPayNLTDate) = 11") tacked on, then I "probably" wouldn't need it. I say "probably" because Access sometimes requires it and sometimes doesn't.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    I've never used CStr() when domain aggregate has multiple criteria.

    Ampersand (&) is the preferred concatenation operator. If cRecordID is a number type field, this should work.

    =Nz(DSum("invValue", "tblTrackingDelivery", "cRecordId=" & [cRecordId] & " AND Month(vPayNLTDate) = 11"), 0)

    If you want the data aggregated before the DSum() then need a query object that the DSum can reference as I show in previous post.

    Don't you also need the year in the criteria?
    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.

  5. #5
    Dave D is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jun 2014
    Posts
    45
    From your reply I take it DSUM doesn't support Group By...

    Two new queries and 24 text boxes later (sum of monies in /out per month), I now have a working report.

    I also went back and changed the "+" to an ampersand, left out the CStr() and it works as advertised.

    Thank you for your help.

    Since I found a suitable workaround (with your help) I'll mark this thread complete.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Not sure what you mean by "doesn't support Group By" - domain aggregate functions must refer to table or query objects. Could be a Group By aggregate query.
    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. Replies: 4
    Last Post: 07-24-2013, 02:34 PM
  2. Replies: 0
    Last Post: 02-25-2013, 04:43 PM
  3. Replies: 2
    Last Post: 06-21-2012, 07:40 PM
  4. Group title carrying forward to next group
    By Amandasr12 in forum Reports
    Replies: 3
    Last Post: 06-06-2012, 02:13 PM
  5. columns for group detail but not group header?
    By Coolpapabell in forum Reports
    Replies: 0
    Last Post: 08-21-2009, 08:53 AM

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