Results 1 to 3 of 3
  1. #1
    mrmims is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    53

    Query sums based on checkbox criteria?????

    Hello,



    I am working with an inventory database. I have three tables:
    1) TBL_Components
    2) TBL_Locations (All places the components are found. Some locations are marked as SpareLocation with a checkbox, all Components in these Locations are Spare. If SpareLocation is not checked then all Components in this Location are considered In Use)
    3) TBL_ComponentConnections (Table that makes the connections between Component and Locations. In this table is the Quantity of each component in that Location)

    I want a single query that will list each Component and two expressions for the quantities, one expression for quantity of each component in a SpareLocation and one expression for quantity of each component not in a SpareLocation. I can do this successfully as two separate queries, that is easy. But how do I write the expressions for all information to show in 1 query?

    I tried 2 DSum expressions but they just returned the complete sum of components, instead of a sum of each individual component.

    This is my SQL Query for Components In Use
    Code:
    SELECT TBL_Components.PartNumber, TBL_Components.Description, Sum(TBL_InventoryConnections.QuantityOnHand) AS SumOfQuantityOnHand, TBL_Location.SpareLocation
    FROM TBL_Location INNER JOIN (TBL_Components INNER JOIN TBL_InventoryConnections ON TBL_Components.ComponentsID = TBL_InventoryConnections.ComponentsID) ON TBL_Location.LocationID = TBL_InventoryConnections.LocationID
    GROUP BY TBL_Components.PartNumber, TBL_Components.Description, TBL_Location.SpareLocation
    HAVING (((TBL_Location.SpareLocation)=False))
    ORDER BY Sum(TBL_InventoryConnections.QuantityOnHand) DESC;
    The SQL for the Spare Components is the same, but SpareLocation is True.

    This is what I tried, but did not work. I get an error saying "You tried to execute a query that does not include the specified expression '[SpareLocation]=0 and [SpareLocation]=-1' as part of an aggregate function.
    Code:
    SELECT TBL_Components.PartNumber, TBL_Components.Description, Sum(TBL_InventoryConnections.QuantityOnHand) AS SumOfQuantityOnHand, Sum(TBL_InventoryConnections.QuantityOnHand) AS SumOfQuantityOnHand1
    FROM TBL_Location INNER JOIN (TBL_Components INNER JOIN TBL_InventoryConnections ON TBL_Components.ComponentsID = TBL_InventoryConnections.ComponentsID) ON TBL_Location.LocationID = TBL_InventoryConnections.LocationID
    GROUP BY TBL_Components.PartNumber, TBL_Components.Description
    HAVING (([SpareLocation]=False) AND ([SpareLocation]=True))
    ORDER BY Sum(TBL_InventoryConnections.QuantityOnHand) DESC , Sum(TBL_InventoryConnections.QuantityOnHand) DESC;
    I have also attached a picture of the query design.

    Thank you for your help, I am sure I am missing something simple.



    Click image for larger version. 

Name:	Query_Criteria.jpg 
Views:	15 
Size:	96.4 KB 
ID:	17138

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Try:

    SELECT TBL_Components.PartNumber, TBL_Components.Description, Sum(IIf(SpareLocation=True, TBL_InventoryConnections.QuantityOnHand, Null)) AS SumOfQtySpare, Sum(IIf(SpareLocation=False, TBL_InventoryConnections.QuantityOnHand, Null)) AS SumOfQtyNotSpare, TBL_Location.SpareLocation
    FROM TBL_Location INNER JOIN (TBL_Components INNER JOIN TBL_InventoryConnections ON TBL_Components.ComponentsID = TBL_InventoryConnections.ComponentsID) ON TBL_Location.LocationID = TBL_InventoryConnections.LocationID
    GROUP BY TBL_Components.PartNumber, TBL_Components.Description, TBL_Location.SpareLocation
    ORDER BY Sum(TBL_InventoryConnections.QuantityOnHand) DESC;
    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
    mrmims is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    53
    SOLVED

    You are my hero, and after looking at it makes complete sense!

    Thank you so much.

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

Similar Threads

  1. Replies: 10
    Last Post: 05-23-2014, 02:29 PM
  2. Replies: 5
    Last Post: 05-01-2013, 11:39 AM
  3. Help about adding up sums using a checkbox.
    By MelonFuel in forum Forms
    Replies: 1
    Last Post: 06-29-2012, 06:16 AM
  4. Query that sums a field based on 2 tables
    By scratchmb in forum Queries
    Replies: 6
    Last Post: 03-18-2012, 08:31 PM
  5. using checkbox as iif criteria
    By TheShabz in forum Queries
    Replies: 3
    Last Post: 01-18-2011, 05:10 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