sorry - really tied up with work at the moment - in outline you need to add a way to 'back up' and then go down the next path.
I've dug out some code I've used in the past to build a tree which you can adapt - it does different things to what you require, but you can follow the logic of the process path - this the equivalent of recCompaniesOwned
Code:
Private Function BuildNodes(TreeID As Long, tblName As String, Optional ProgressBox As Control)
Dim SQLStr As String
Dim rst2 As Recordset
While TreeLevel <> 0 'LftRgt < MaxLft
'find children at next level
SQLStr = "SELECT TOP 1 [" & tblName & "].EntityPK FROM (dtaRelationships INNER JOIN [" & tblName & "] ON dtaRelationships.EntityFK = [" & tblName & "].ParentFK) LEFT JOIN dtaRelationships AS dtaRelationships_1 ON [" & tblName & "].EntityPK = dtaRelationships_1.EntityFK WHERE dtaRelationships.[TLevel]= " & TreeLevel & " AND dtaRelationships_1.EntityFK Is Null"
Set rst2 = CurrentDb.OpenRecordset(SQLStr, dbOpenSnapshot)
LftRgt = LftRgt + 1
If LftRgt Mod 100 = 0 Then
If Not ProgressBox Is Nothing Then ProgressBox = Replace(ProgressBox, "Processed " & OldValue, "Processed " & LftRgt)
OldValue = LftRgt
End If
If Not rst2.EOF Then
SQLStr = "INSERT INTO dtaRelationships ( EntityFK, Lft, TLevel, TreeID) VALUES (" & rst2!EntityPK & ", " & LftRgt & ", " & TreeLevel - 1 & ", " & TreeID & ")"
'sqlStr = "INSERT INTO dtaRelationships ( EntityFK, Lft, TLevel, TreeID) SELECT EntityPK, " & LftRgt & ", " & TreeLevel - 1 & ", " & TreeID & " FROM [" & tblName & "] WHERE EntityPK = " & rst2!EntityPK
CurrentDb.Execute SQLStr, dbFailOnError
TreeLevel = TreeLevel - 1
Else
SQLStr = "UPDATE dtaRelationships SET rgt= " & LftRgt & ", TLevel = -TLevel WHERE TLevel=" & TreeLevel
CurrentDb.Execute SQLStr, dbFailOnError
TreeLevel = TreeLevel + 1
End If
Set rst2 = Nothing
Wend
End Function
and this the equivalent of getowned
Code:
Public Sub BuildNSM(Optional tblName As String = "dtaEntities", Optional ProgressBox As Control)
Dim SQLStr As String
Dim nsm As DAO.Recordset
Dim TreeID As Long
'set the stage
UpdateProgress " Clearing Relationships" & vbCrLf, ProgressBox
CurrentDb.Execute ("DELETE * FROM dtaRelationships")
MaxLft = 2 * DCount("*", tblName)
d = Now()
On Error Resume Next
UpdateProgress " Start: " & MaxLft & " " & Format(Now(), "hh:mm:ss") & vbCrLf, ProgressBox
OldValue = LftRgt
d = Now()
'set ultimate parents
Set nsm = CurrentDb.OpenRecordset("SELECT EntityPK FROM [" & tblName & "] WHERE ParentFK=0", dbOpenSnapshot)
LftRgt = 1
TreeID = 1
While Not nsm.EOF
TreeLevel = -1
SQLStr = "INSERT INTO dtaRelationships (EntityFK, Lft, TLevel, TreeID) VALUES(" & nsm!EntityPK & ", " & LftRgt & ", " & TreeLevel & ", " & TreeID & ")"
CurrentDb.Execute (SQLStr)
UpdateProgress " Processed " & LftRgt & vbCrLf, ProgressBox
OldValue = LftRgt
BuildNodes TreeID, tblName, ProgressBox
TreeID = TreeID + 1
nsm.MoveNext
Wend
UpdateProgress " Finished: " & Format(Now(), "hh:mm:ss") & " Elapsed time: " & Format(Now() - d, "hh:mm:ss") & vbCrLf, ProgressBox
Set nsm = Nothing
End Sub