Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6925561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:48:18+00:00 2026-05-27T10:48:18+00:00

I’m using MS Access with a MySQL database. One table has records which represent

  • 0

I’m using MS Access with a MySQL database. One table has records which represent ‘documents’:

Table: doc
Primary Key: doc_id

These records can be linked together via a link table:

Table: link
Primary Key: link_id
Foreign Key: doc_id_A
Foreign Key: doc_id_B

Thus the records may be linked in a chain, eg Doc A linked to Doc B, which is linked to Doc C, etc, and also, Doc A may be linked to any number of other documents.

In reality, the ‘family’ of inter-related documents wouldn’t exceed 20 records.

I’m looking for an efficient MySQL proc, or VBA function – or a query – to find all the members of a ‘family’ for one specified record.

Any suggestions would be most welcome!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:48:19+00:00Added an answer on May 27, 2026 at 10:48 am

    So the link table gives a self join to doc, you absolutely need a doc to be able to:

    1. Link to itself
    2. … as many times as it likes
    3. Link to another doc
    4. … multiple times
    5. Be separately linked to by the same other doc
    6. … multiple times
    7. Store information about the link

    So your link table could have 10 separate links 1-1, 1-1, 1-1, 1-2, 1-2, 1-2, 2-1, 2-1, 2-1, 2-2 etc. with just 2 docs in the ‘family’.


    I expect when you look at this list you will probably think that you don’t need most of them, a lot of the inefficiency in your solution might be coming from this unnecessary flexibility. My favoured suggestion would be to start with a strict hierarchy and build minimally from there.

    But here is my answer anyway, it’s tested and working in Access-2010 and local tables. ADODB should work just as well with linked tables.

    Option Compare Database
    Option Explicit
    Const MaxInFamily = 30
    
    'Requires a reference to "Microsoft ActiveX Data Objects 2.x Library" (VBA Menu: Tools, references)
    
    Function GetFamily(id As Long) As Long()
    Dim Found(MaxInFamily) As Long
    Dim MaxFound As Integer
    Dim CurrentSearch As Integer
    Dim Sql As String
    Dim rs As New ADODB.Recordset
    
        Found(1) = id
        MaxFound = 1
    
        For CurrentSearch = 1 To MaxInFamily
    
            If CurrentSearch > MaxFound Then Exit For
    
            Sql = "SELECT doc_id_2 as NewID FROM link WHERE doc_id_1 = " & Found(CurrentSearch) _
                    & " AND doc_id_2 NOT IN (" & ArrayToCsv(Found, MaxFound) & ")" _
                    & " UNION " _
                    & " SELECT doc_id_1 FROM link WHERE doc_id_2 = " & Found(CurrentSearch) _
                    & " AND doc_id_1 NOT IN (" & ArrayToCsv(Found, MaxFound) & ")"
    
            rs.Open Sql, CurrentProject.Connection
    
            Do While Not rs.EOF
                MaxFound = MaxFound + 1
                Found(MaxFound) = rs("NewID").Value
                rs.MoveNext
            Loop
    
            rs.Close
    
        Next CurrentSearch
    
        GetFamily = Found
    
    End Function
    
    Function ArrayToCsv(SourceArray() As Long, ItemCount As Integer) As String
    Dim Csv As String
    Dim ArrayIndex As Integer
    
        For ArrayIndex = 1 To ItemCount
            Csv = Csv & SourceArray(ArrayIndex)
            If ArrayIndex < ItemCount Then Csv = Csv & ", "
        Next ArrayIndex
    
        ArrayToCsv = Csv
    
    End Function
    

    Duplicated results and queries are avoided by excluding items already found at the server, and as the link is unidirectional I’ve used a UNION query to look both ways at once. At most it’ll do MaxInFamily round trips to the server.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a reasonable size flat file database of text documents mostly saved in
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.