Click here to Skip to main content
16,020,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to retrieve hierarchical records from MS Access database in a Select Query. Can anyone help me ?
Like CONNECT TO PRIOR IN Oracle , I want something like that in MS Access.

I have column names and records like this:

FIRST_NAME LAST_NAME FAMILY_ID PARENT_ID Path
-----------------------------------------------------------------------------
Quackmore Duck 201 200 /Quackmore
Thelma Duck 203 201 /Quackmore/Thelma
Huey Duck 205 203 /Quackmore/Thelma/Huey
Dewey Duck 206 203 /Quackmore/Thelma/Dewey
Louie Duck 207 203 /Quackmore/Thelma/Louie
Donald Duck 204 201 /Quackmore/Donald


The output should be like this:


FIRST_NAME LAST_NAME FAMILY_ID PARENT_ID PARENT_NAME
----------------------------------------------------------------------------
Quackmore Duck 201 200
Thelma Duck 203 201 Quackmore
Huey Duck 205 203 Thelma
Dewey Duck 206 203 Thelma
Louie Duck 207 203 Thelma
Donald Duck 204 201 Quackmore


And the hierarchical structure depth may be upto any level.
Please someone help me quickly.

Thanking you in advance... Please ....please...
Posted
Updated 25-Sep-18 1:32am
v3

Not supported in Access. Access also doesn't support stored procedures.

To do this in Access, you'll have to get the top level records, then iterate over those and query for the children, iterate over those and query for children, ..., until you don't get any records back.
 
Share this answer
 
I'm not sure how relevant this might be...

VB
Private Sub LoadData
   'create datatable to fill with edited results
   Dim MyFixedTable as new datatable("edited results")
   'specify search criteria
   Dim colToSearch as String = "LAST_NAME"
   Dim searchingFor as String = "Duck"
   Dim expressionString as String = colToSearch & " LIKE '" & searchingFor & "'"

   '.select
   Dim returnedDataRows() as DataRow = 
      DatabaseForm.MyDataSet.MyDataBase.Select(expressionString)

   'if results found, fix results into new table
   if returnedDataRows.Length > 0
      For idx = 0 to returnedDataRows.length - 1
          Dim DataRow2Send as DataRow = returnedDataRows(idx)
          FillMyFixedTable(MyFixedTable, DataRow2Send)
      Next
      FillInParents(MyFixedTable)
   End If
End Sub

Private Sub FillMyFixedTable(ByRef FixedTable as DataTable, byval NewRow as DataRow)
   Dim newDRow as FixedTable.NewRow
   newDRow.Item(0) = NewRow.item(0)
   newDRow.Item(1) = NewRow.item(1)
   newDRow.Item(2) = NewRow.item(2)
   newDRow.Item(3) = NewRow.item(3)
   FixedTable.Rows.Add(newDRow)
End Sub

Private Sub FillInPArents(ByRef FixedTable as DataTable)
   for idx = 0 to FixedTable.Rows.Count - 1
       dim ParentTableRow as dataRow = FixedTable.Rows.Find(FixedTable.Rows(idx).Item(3))
       if not ParentTableRow is nothing
           FixedTable.Rows(idx).item(4) = ParentTableRow.Item(0)
       else
           FixedTable.Rows(idx).item(4) = "" 
       end if
   Next
End Sub
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900