Results 1 to 4 of 4
  1. #1
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31

    Excluding all records in a many-to-many relationship


    Click image for larger version. 

Name:	Capture.JPG 
Views:	9 
Size:	47.3 KB 
ID:	16552

    I have a trivia database and I want to keep track of what questions were used at different establishments, so that I can avoid using the same question more than once at a single establishment.

    Say for example, Question 1 was used at both pub A and pub B. I then want to build a query that shows me all of the questions that have NOT been used at pub B. I build the query seen above and in the Criteria for the ClientName field I put 'not "pub B" '. The problem with this query is that Question 1 has also been used at pub A, so it will return Question 1 in the results - not what I want given that Question 1 has already been used at pub B.

    Basically I would like to know how to build a query that excludes ALL of the question ID's that have any association with pub B. I have tried building a query with a subquery, but gotten some wonky results.

  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,772
    Try:

    Query1
    SELECT CompletedTrivias.CustomerID, UsedQuestions.QuestionID
    FROM CompletedTrivias RIGHT JOIN UsedQuestions ON CompletedTrivias.ID = UsedQuestions.TriviaID;

    Query2
    SELECT Clients.ID, Questions.ID
    FROM Clients, Questions;

    Query3
    SELECT Query2.Clients.ID, Query2.Questions.ID, Query1.QuestionID
    FROM Query2 LEFT JOIN Query1 ON (Query2.Questions.ID = Query1.QuestionID) AND (Query2.Clients.ID = Query1.CustomerID)
    WHERE (((Query2.Clients.ID)=[enter client ID]) AND ((Query1.QuestionID) Is Null));
    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
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31
    Thank you, that works perfectly. I will need to work on becoming more adept at using joins.

  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,772
    And when not to use joins. Note that Query2 does not have a join clause (these two tables do not a natural relationship and couldn't be joined anyway). This results in a Cartesian relationship where every record in each table joins with every record of other table to provide a dataset of all possible Client/Question pairs. Then Query3 is essentially a FindUnmatched to determine which of the pairs does not occur in Query1.
    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: 6
    Last Post: 12-11-2013, 01:23 PM
  2. Query is excluding records with blank fields
    By Menelaus in forum Queries
    Replies: 1
    Last Post: 03-28-2013, 08:14 PM
  3. Query for excluding TOP N records
    By dolovenature in forum Queries
    Replies: 1
    Last Post: 09-14-2012, 03:49 PM
  4. Replies: 8
    Last Post: 08-13-2012, 04:30 AM
  5. Excluding records that contain wildcards
    By AccessUser123 in forum Programming
    Replies: 3
    Last Post: 06-09-2011, 08:17 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