Results 1 to 4 of 4
  1. #1
    pharmacologist is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2019
    Posts
    39

    How to find all related records in one table from a "lineage" of related records in another table


    I'll try to explain better if this doesn't make sense. I have records in a table we'll call "Members" with a field called "ParentID" which indicates the parent of the record (which exists in the same table). Those parents also have a ParentID listed in their and so on so there are different lineages of related records. I have another table that relates to the primary key of the Members table and I would like to be able to pull all the records from that second table that are associated with any specified primary key of Members and all of the parent records and/or all of the children records (i.e. Member records for which their ParentID matches to follow the lineage downward). I hope that makes sense. Basically it's like a family tree and I want to be able to get all the records from one table that are related to a primary key and all it's relatives in another table. I'm pretty novice with SQL commands and can't think of how this would be possible.

    Thanks for all help!

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    Not really following the description of what you have and what you want. Always better to provide some example data to illustrate what you have, what you want to do and the outcome required.

    Best guess is what you are taking about is a recursive query. These exist in sql server but not ace or jet (the default access dB). It can be done but you will need to use vba

  3. #3
    pharmacologist is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2019
    Posts
    39
    OK, here's a basic design of the tables

    Members DesiredTable
    ID Parent ID ID MemberID DesiredField
    1 - 1 1 A
    2 1 2 2 B
    3 2 3 2 C
    4 3 4 3 D
    5 2 5 4 E
    6 5 6 5 F
    7 5 G
    8 6 H

    Here's what I'm looking to do.

    1) Select all records from table DesiredTable where MemberID matches Members.ID = 5 and all family members within its lineage, so it would give me:

    Results
    DesiredTable.ID MemberID DesiredField
    1 1 A
    2 2 B
    3 2 C
    6 5 F
    7 5 G
    8 6 H

    2) Select all records from table DesiredTable where MemberID matches Members.ID = 3 and all family members above its lineage (i.e. parents, grandparents etc), so it would give me:

    Results
    DesiredTable.ID MemberID DesiredField
    1 1 A
    2 2 B
    3 2 C
    4 3 D

    3) Select all records from table DesiredTable where MemberID matches Members.ID = 3 and all family members below its lineage (i.e. children, grandchildren etc), so it would give me:

    Results
    DesiredTable.ID MemberID DesiredField
    4 3 D
    5 4 E


    I'm very comfortable with VBA and everything I'm doing with this is already coded in VBA so that's no problem and in fact the type of solution I'm looking for.

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    you will definitely need to use a recursive function. suggest google 'vba recursive function' or 'vba tree functions' to get some ideas

    you can also search on this and other forums for examples that might more closely meet your needs

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

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2021, 08:02 AM
  2. Replies: 2
    Last Post: 01-23-2017, 07:06 PM
  3. Replies: 4
    Last Post: 04-21-2014, 01:18 PM
  4. Replies: 3
    Last Post: 03-22-2014, 04:32 PM
  5. Replies: 10
    Last Post: 10-10-2012, 11:15 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