ACC: Sample Procedure to Fill a TreeView Control RecursivelyID: Q167309
|
This article first explains recursive procedures and how you can use them
in Microsoft Access. Then, it shows you how to use a recursive procedure to
fill branches of a TreeView control with data.
The TreeView control is available with the Microsoft Office 97 Developer
Edition Tools and the Microsoft Access Developer's Toolkit version 7.0.
This article assumes that you are familiar with Visual Basic for
Applications and with creating Microsoft Access applications using the
programming tools provided with Microsoft Access. For more information
about Visual Basic for Applications, please refer to your version of the
"Building Applications with Microsoft Access" manual.
The technique of recursion is defined as a procedure that calls itself in the middle of its routine. Following is a short example of a recursive function that returns the first file name that matches a user's input. The function prompts for a path and file name, and then uses the Dir() function to verify that the file exists. If the Dir() function returns an empty string (""), the file does not exist and the recursive procedure calls itself again. The second instance of the procedure prompts for a path and file name again, tests the input, and passes the results back to the first instance of the procedure. The following sample function continues to call itself recursively until a user types a valid path and file name:
Function FirstFileMatch()
Dim strFileName as String
On Error Resume Next
strFileName = Dir(InputBox("Enter a valid path and file name."))
If strFileName = "" Then ' Bad input. No Match.
FirstFileMatch = FirstFileMatch() ' Here is the recursive call.
Else ' This is the condition that ends this recursive loop.
FirstFileMatch = strFileName ' Return value to calling function.
End If
End Function
The recursive procedure continues to call itself until some condition is
satisfied, in this case until the user's input matches a file name on the
hard drive. Once there is a match, the results are passed back to the
instance of the procedure that called it. Then, that instance of the
procedure passes results back to the previous instance, and so on, until
focus returns to the top level instance of the procedure.
TreeView control:
Name: xTree
Width: 4"
Height: 3"
'=================Load Event for the Form=======================
'Initiates the routine to fill the TreeView control
'===============================================================
Private Sub Form_Load()
Const strTableQueryName = "Employees"
Dim db As Database, rst As Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset(strTableQueryName, dbOpenDynaset, _
dbReadOnly)
AddBranch _
rst:=rst, _
strPointerField:="ReportsTo", _
strIDField:="EmployeeID", _
strTextField:="LastName"
End Sub
'================= AddBranch Sub Procedure =========================
' Recursive Procedure to add branches to TreeView Control
'Requires:
' ActiveX Control: TreeView Control
' Name: xTree
'Parameters:
' rst: Self-referencing Recordset containing the data
' strPointerField: Name of field pointing to parent's primary key
' strIDField: Name of parent's primary key field
' strTextField: Name of field containing text to be displayed
'===================================================================
Sub AddBranch(rst As Recordset, strPointerField As String, _
strIDField As String, strTextField As String, _
Optional varReportToID As Variant)
On Error GoTo errAddBranch
Dim nodCurrent As Node, objTree As TreeView
Dim strCriteria As String, strText As String, strKey As String
Dim nodParent As Node, bk As String
Set objTree = Me!xTree.Object
If IsMissing(varReportToID) Then ' Root Branch.
strCriteria = strPointerField & " Is Null"
Else ' Search for records pointing to parent.
strCriteria = BuildCriteria(strPointerField, _
rst.Fields(strPointerField).Type, _
"=" & varReportToID)
Set nodParent = objTree.Nodes("a" & varReportToID)
End If
' Find the first emp to report to the boss node.
rst.FindFirst strCriteria
Do Until rst.NoMatch
' Create a string with LastName.
strText = rst(strTextField)
strKey = "a" & rst(strIDField)
If Not IsMissing(varReportToID) Then 'add new node to the parent
Set nodCurrent = objTree.Nodes.Add(nodParent, _
tvwChild, strKey, strText)
Else ' Add new node to the root.
Set nodCurrent = objTree.Nodes.Add(, , strKey, _
strText)
End If
' Save your place in the recordset so we can pass by ref for
' speed.
bk = rst.Bookmark
' Add employees who report to this node.
AddBranch rst, strPointerField, strIDField, strTextField, _
rst(strIDField)
rst.Bookmark = bk ' Return to last place and continue search.
rst.FindNext strCriteria ' Find next employee.
Loop
exitAddBranch:
Exit Sub
'--------------------------Error Trapping --------------------------
errAddBranch:
MsgBox "Can't add child: " & Err.Description, vbCritical, _
"AddBranch Error:"
Resume exitAddBranch
End Sub
strPointerField:="<Field name that points to a parent record>"
strIDField:="<Name of the PrimaryKey Field>"
strTextField:="<Field name whose data you want to display>"
For more information about using the TreeView control, search the Help
Index for "TreeView control."
For more information about recursive procedures, please see the following
articles in the Microsoft Knowledge Base:
Q132242 ACC2: Sample Function Using Recursion to Display Data Tree
Q165993 ACC97: Example Using TreeView Control Drag-and-Drop Capabilities
Additional query words: Recursion Directory Tree
Keywords : kbprg
Version : WINDOWS:7.0,97
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: August 5, 1999