Damian3215
Programmer
How would I modify this code to get me the Parent one level up from the current record?
Then how could I create a path from the highest level to the current record?
It would visually look something like this.
Name Path
Mac OS 10.1 (Cheetah) Home // Apple // Apple OS // OS X // Cheetah (Mac OS 10.1)
Code:
Public Function getTopParent(varID As Variant) As Long
Dim rs As DAO.Recordset
Dim parentID As Variant
Dim strSql As String
strSql = "Select parentID from tblEquipment WHERE ID = " & varID
Set rs = CurrentDb.OpenRecordset(strSql)
parentID = rs!parentID
If IsNull(parentID) Then
getTopParent = varID
Exit Function
Else
getTopParent = getTopParent(parentID)
End If
End Function
Then how could I create a path from the highest level to the current record?
It would visually look something like this.
Name Path
Mac OS 10.1 (Cheetah) Home // Apple // Apple OS // OS X // Cheetah (Mac OS 10.1)
Code:
Function Path(ByVal id As Long)
' Builds a human-readable path from the root node to the current node.
' This can be done by recursion or by a loop. The loop has been selected here
' and limits the maximal depth of the returned path using a For/Next loop.
Dim i As Integer
Path = Null
With GetTable("tblZones")
For i = 1 To MAX_DEPTH
.Seek "=", id
If .NoMatch Then Exit Function
' append the name to the front of the path
Path = Nz(!ChildName, !Name) & " // " + Path
' expected exit point, at the root node!
If IsNull(!ParentID) Then Exit Function
' continue with parent node
id = !ParentID
Next i
End With
' if this line is reached, the For/Next was exhausted
Path = "... // " + Path
End Function