Results 1 to 2 of 2
  1. #1
    David Randle is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2016
    Posts
    6

    Using Dsum To Show Individual Product Totals in a Report

    Hi

    I have a report that shows all items contained in a stock table.

    I am trying to use the dsum function with two criteria to provide the current stock holding of each individualproduct in the report, (line by line).

    The criteria are based on a unique field (productId) and a number (inOrOut) which represents whether the stock has been added or deleted.

    The expression I am using is:

    = DSum("Quantity", "StockTbl", "ProductId = " & [Reports]![StockMovementRep]![ProductId] _
    & "AND InOrOut = 1") - DSum("Quantity", "StockTbl", "ProductId = " & [Reports]![StockMovementRep]![ProductId] _
    & "AND InOrOut = 2")

    I have used same expression, using [Forms] to populate a field on a form and it works fine. However, it does not do the same thing on the report.



    Would appreciate some assistance with this.

    Many thanks

    David

  2. #2
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Several issues could be at play here, some of which might not rear their ugly heads until you solve the one before the next issue. More information about what you are doing may be necessary if none of this solves the issue.
    1) you provide the data type for one field ... and a number (inOrOut) but not the other. If ProductId is text, you are missing the necessary quote marks.

    2) since there are many ways and places to put calculated controls on a form or report, your prior success may not be something that you can replicate here by simply employing similar syntax in an expression. The method to use depends on things like where the calculated control is placed (header, footer or detail section) and what section of the report/form the source controls are. If your calculation a line item (i.e. you want it to appear line by line) this is best done in the report's underlying query AFAIC. Your references would then be based on table fields, not report controls. To do this line by line in a report anyway, you should not need the reference to the report object, just the fields of the underlying recordset ([controlName1] - [controlName2]). I believe the DSum function is not the right choice for a line by line calculation, since that is meant to be used over a domain. Your post suggests the calculation is being done line by line.

    3) if you use a wizard to build a report or form, Access will name your controls the same as their related fields. When attempting to add aggregate functions in calculated controls that you add afterwards, Access sometimes does not handle the calculations because of this. I have not read or determined if Access fails on the calculation because of the ambiguity (no error is generated) or if it's because a referenced control has no data when the calculation is made (but the field does). I will use a wizard, but I ALWAYS go through the controls and rename them using my naming convention before doing anything else in order to prevent this. I cannot tell if your controls and their bound fields are named the same.
    Last edited by Micron; 03-06-2016 at 10:37 PM. Reason: (but the field does)
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 2
    Last Post: 10-26-2015, 09:11 AM
  2. Replies: 6
    Last Post: 02-28-2015, 11:57 AM
  3. Totals queries with multiple product types
    By mike_980 in forum Queries
    Replies: 1
    Last Post: 03-28-2014, 01:42 PM
  4. Replies: 1
    Last Post: 08-28-2013, 02:27 AM
  5. Replies: 6
    Last Post: 02-28-2013, 05:38 PM

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