Results 1 to 3 of 3
  1. #1
    dth122 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    20

    Subquery question

    I'm having a problem rolling a subquery into a query.



    I have two tables, tblAllCells and tblPlots. tblPlots is a subset of tblAllCells. They linked by SiteNumber.

    I have a subquery that operates on tblPlots with an IIf that sets a value "Status" based on the existence of various values in the fields of tblPlots. It sets the Status to "A" if none of the conditions are met. This is working fine. (so far)

    subqryStatus:
    SELECT tblPlots.SiteNumber, IIf([TakerNum]>0,"O",IIf([tblPlots.OwnerNum]>0,"S",IIf([HoldFor] Is Not Null,"H","A"))) AS Status
    FROM tblOwners RIGHT JOIN (tblPlots LEFT JOIN qryUsage ON tblPlots.SiteNumber = qryUsage.SiteNumber) ON tblOwners.OwnerNum = tblPlots.OwnerNum
    ORDER BY tblPlots.SiteNumber;




    I then have a main query where I join the subquery and tblAllCells. I'm looking for a result that includes all of the SiteNumbers from tblAllCells, with the Status from the subquery for any matching SiteNumbers in tblPlots and a null Status for any SiteNumbers that are in tblAllCells but not in tblPlots. (though I would also take some other status if it could set it)

    qryStatus:
    SELECT tblAllCells.SiteNumber, subqryStatus.Owner, subqryStatus.Usage, subqryStatus.HoldFor, subqryStatus.Status
    FROM tblAllCells LEFT JOIN subqryStatus ON tblAllCells.SiteNumber = subqryStatus.SiteNumber;


    I'm getting the correct rows from tblAllCells, but I'm seeing a status of "A" (my "else" path from the subquery) for the SiteNumbers in tblAllCells that aren't in tblPlots. What I expect is a null.

    I'm baffled as to how the IIf expression in the subquery can operate on rows of tblAllCells when it's not even included in the subquery.

    What am I missing?
    - Dave

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,523
    I advise not to use IIFs in the query. instead, use a lookup table joined to the data table.
    this performs the IIF.

  3. #3
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,784
    Not sure the IIF is relevant to the problem or not, but one thing you can do is make a table from that query and examine all the data for issues. Then join that table to your main query and play with the results. What that will do is either confirm or eliminate the possibility that the issue is the expression. It could also be something as simple as not using the right join, but then again we have no results vs input to look at.
    FYI - what you are describing is a nested query. A sub query is something else all together.
    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. A subquery inside a subquery
    By tbjmobile@gmail.com in forum Queries
    Replies: 15
    Last Post: 04-22-2018, 11:48 AM
  2. Subquery
    By Alex Motilal in forum Queries
    Replies: 1
    Last Post: 03-08-2018, 03:11 AM
  3. Subquery Help
    By MTSPEER in forum Queries
    Replies: 1
    Last Post: 12-05-2017, 01:57 PM
  4. Subquery
    By tomclavil in forum Queries
    Replies: 3
    Last Post: 02-27-2012, 03:05 AM
  5. Replies: 1
    Last Post: 02-01-2010, 06:12 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