Results 1 to 2 of 2
  1. #1
    vgillis is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    3

    Using a table in a query multiple times.

    Hi all.

    I have a question.


    I have a table...lets call it name1.

    I have another table called Restictions. This is a one to many relationship.

    What I want to do is have a record based on criteria pulled from the restrictions table so that each critieria is in a seperate field.

    This is what I have..

    SELECT Restriction.Restriction, Name1.Name1, Restriction_1.Restriction, Restriction_2.Restriction, Restriction_3.Restriction
    FROM (((Name1 LEFT JOIN Restriction ON Name1.ID = Restriction.[Id Number]) LEFT JOIN Restriction AS Restriction_1 ON Name1.ID = Restriction_1.[Id Number]) INNER JOIN Restriction AS Restriction_2 ON Name1.ID = Restriction_2.[Id Number]) LEFT JOIN Restriction AS Restriction_3 ON Name1.ID = Restriction_3.[Id Number]
    WHERE (((Restriction.Restriction)="NOC") AND ((Restriction_1.Restriction)="AEM") AND ((Restriction_2.Restriction)="AMC") AND ((Restriction_3.Restriction)="APC"));

    So I always want the name record. and only the restriction record that meets the criteria. As it is, it only brings me a record if there is a restriction record for every value. Which is not always the case.
    If i change the ANDs to ORs..I get strange results.

    Any help would be appreicated!

    Thanks.
    Veronica

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Name1.ID is the foreign key field for Restriction.[ID Number]?

    You have an INNER JOIN which means data must be in both tables.

    Not sure duplicate joins needed.

    SELECT Name1, IIf([Restriction]="NOC",[Restriction],Null) As ResNOC, IIf([Restriction]="AEM",[Restriction],Null) As ResAEM, IIf([Restriction]="AMC",[Restriction],Null) As ResAMC, IIf([Restriction]="APC",[Restriction],Null) As ResAPC FROM Name1 LEFT JOIN Restriction ON Name1.ID = Restriction.[ID Number] WHERE Restriction = "NOC" OR Restriction = "AEM" OR Restriction = "AMC" OR Restriction = "APC";

    This technique 'pivots' the data without CROSSTAB query. Of course, CROSSTAB is an available option.
    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: 2
    Last Post: 04-04-2012, 03:52 AM
  2. Run query multiple times on different tables
    By dumbledown in forum Queries
    Replies: 2
    Last Post: 03-14-2012, 05:39 AM
  3. Form_Current() running multiple times
    By caddcop in forum Forms
    Replies: 0
    Last Post: 04-01-2011, 09:00 AM
  4. Replies: 1
    Last Post: 07-12-2010, 12:00 PM
  5. Replies: 5
    Last Post: 12-10-2009, 10:33 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