Results 1 to 11 of 11
  1. #1
    ferrets is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    7

    Linking a table to itself


    I have a table and I need to link records within that table to other records within the same table. So for example a people table and I want to be able to link John to Andrew, Mike to John, Andrew to Mike etc.

    When I bring up a record in a form I need to be able to see in the subform everybody who is linked to them. If I set up a link table with Person_ID1, and Person_ID2 then I am guessing that I would only see John's link to Andrew, but I wouldn't see Mike's link to John until I opened up Mike's record. Is there anyway I can set this up so that I can see all of John's linked friends regardless of whether John appears in the first or second link record?

    Hope that makes sense!

  2. #2
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    So given a form opened up on the record for John how about a subform based on a query like this:
    SELECT * FROM [you're bridge table name here] WHERE Person_ID1 = [John's ID here] OR Person_ID2 = [John's ID here];

    The [John's ID here] part would be a reference to a relevant ID field on the parent form.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    In a query, you'd add the person table twice. Access will alias the second instance with something like Person_1, which you can change. Then you join each of your fields to a different instance of the table.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    Quote Originally Posted by pbaldy View Post
    In a query, you'd add the person table twice. Access will alias the second instance with something like Person_1, which you can change. Then you join each of your fields to a different instance of the table.
    I hope I'm not out of line for asking questions even though I'm not OP, but now I'm slightly confused.

    I'm guessing the problem is they're using a many-to-many relationship table, a person's primary key could be found in one of two different foreign key fields in the many-to-many table. So what's the best way to to select ALL of a person's friends?

    Maybe like this:

    Code:
    SELECT Friendships.PersonID1 AS FriendID
    FROM Friendships
    WHERE Friendships.PersonID2=[Forms]![Person]![ID]
    
    UNION
    
    SELECT Friendships.PersonID2 AS FriendID
    FROM Friendships
    WHERE Friendships.PersonID1=[Forms]![Person]![ID]
    or this:

    Code:
    SELECT IIf([PersonID1]=[Forms]![Person]![ID],[PersonID2],[PersonID1]) AS FriendID
    FROM Friendships
    WHERE Friendships.PersonID1=[Forms]![Person]![ID] OR Friendships.PersonID2=[Forms]![Person]![ID]
    I'll shut up now until OP chimes back in.

  5. #5
    ferrets is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    7
    That's right. I can see all of John's links where he appears in PERS_ID1 but I can't see his links when appears in PERS_ID2 (at least not when looking at his record in the main form and looking at his links in the subform). Is it possible to post a test version of the database so that perhaps you could have a look?

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    You should be able to attach it after a compact/repair and zipping.

    @kd, no you're not out of line and I may have misunderstood the question. Wouldn't be the first time, nor likely the last.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    ferrets is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    7
    If you open up the People form field you will see that John is linked to Steve and Matthew. But when you go to record 2 you see that Allan is also linked to John.

    What I need to be able to see is everybody that John is linked to, whether he appears in Pers_ID1 or Pers_ID2. SO when I look at John's record I would like to be able to see links to Allan, Steve and Matthew.

    People Links.zip
    People Links.zipPeople Links.zip

  8. #8
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,930
    use a union query

    Select PersA, PersB FROM tblPersons WHERE PersA='John'
    UNION Select PersB, PersA FROM tblPersons WHERE PersB='John'

  9. #9
    ferrets is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    7
    Quote Originally Posted by Ajax View Post
    use a union query

    Select PersA, PersB FROM tblPersons WHERE PersA='John'
    UNION Select PersB, PersA FROM tblPersons WHERE PersB='John'
    Apologies, I'm not being very clear. I use John as an example. I need a form with a subform. For whatever person is in the top of the form, I need to be able to see all of their links in the subform, regardless of whether that person appears in the link table as Pers_ID1 or Pers_ID2

  10. #10
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,930
    the union query would be the recordsouce to your subform with the first column being the link to the record in the main form. You may need to add other fields to display in the subform, I'm just providing the principle you would use

  11. #11
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,930
    Based on your attachment, the recordsource for your subform would be

    Code:
    SELECT  *
    FROM (SELECT PERS1_ID, PERS2_ID, Link_Type FROM People_Link
    UNION SELECT PERS2_ID, PERS1_ID, Link_Type FROM People_Link)  AS R INNER JOIN People ON R.Pers2_ID = People.Pers_ID;
    my example is highlighted in blue

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

Similar Threads

  1. linking to one table OR another
    By jeffh in forum Access
    Replies: 4
    Last Post: 12-27-2017, 03:38 PM
  2. Replies: 29
    Last Post: 05-02-2017, 04:38 PM
  3. Linking table
    By TDSRU in forum Access
    Replies: 18
    Last Post: 03-12-2013, 07:36 PM
  4. BE / FE new table linking
    By jordanturner in forum Access
    Replies: 3
    Last Post: 10-22-2010, 10:48 AM
  5. Table linking
    By emccalment in forum Access
    Replies: 7
    Last Post: 01-28-2010, 03:51 PM

Tags for this Thread

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