ACC: Sample Procedure to Fill a TreeView Control RecursivelyLast reviewed: August 29, 1997Article ID: Q167309 |
The information in this article applies to:
SUMMARYAdvanced: Requires expert coding, interoperability, and multiuser skills. This article first explains recursive procedures, and how you can use them in Microsoft Access. Then, it gives an example that demonstrates a method for using 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.
MORE INFORMATIONThe 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 FunctionThe 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. Recursion is an elegant way to handle data structures, such as linked lists and binary trees. It simplifies the logic and, in most cases, reduces the number of programming lines in your code. Recursion is also an ideal method for handling self-referencing tables. Self-referencing tables contain records that are linked to other records in the same table. The Employees table in the sample database Northwind.mdb is an excellent example of a self-referencing table. The ReportsTo field in the Employees table contains a number that corresponds to the EmployeeID field of the same table. To find the supervisor for any employee, check the number in the employee's ReportsTo field, and then find the employee with that same number in the EmployeeID field. That supervisor also has a ReportsTo field that may contain another employee's EmployeeID number. That employee, in turn, may report to someone else, and so on, until you reach an employee who does not report to anyone. You can use a recursive procedure to display this chain of command in a TreeView control. As the procedure adds each node (employee) to the TreeView control, it calls another instance of itself to add child nodes for all employees who report to that employee. As the procedure adds each child node, it calls another instance of itself to add nodes for those employees who report to that employee, and so on, until it reaches the bottom of the chain. The example below is a recursive procedure that does just that. The AddBranch procedure below accepts five parameters:
Follow these steps to fill a TreeView control with a hierarchical list of employees using a recursive procedure. Employees are added to the tree according to the EmployeeID in the ReportsTo field of the Employees table. CAUTION: Following the steps in this example will modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and perform these steps on a copy of the database.
Comments About the CodeThe AddBranch procedure is a modular routine that you can use in your database without any modifications. However, you must modify the procedure in the OnLoad event of the form to customize it for your database:
REFERENCESFor 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:
ARTICLE-ID: Q132242 TITLE : ACC2: Sample Function Using Recursion to Display Data Tree ARTICLE-ID: Q165993 TITLE : ACC97: Example Using TreeView Control Drag-and-Drop Capabilities |
Additional query words: Recursion Directory Tree
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |