kamandoyle
New member
- Local time
- Today, 21:10
- Joined
- Oct 24, 2014
- Messages
- 2
Hello guys and gals, first post here so be nice! 
Right, I have a form with a Treeview in. I have it populated from a self-referencing database using the following code.
The database this is referencing is about 30000 lines and it takes ~4 minutes to populate this way. I know Treeview isn't really supposed to be used in this way however it's what is required.
Now I have come up with the theory that I will populate each node with children as its clicked to be expanded. This in theory should drive down loading times as it's only populating what is being looked at, I however have no idea how to do this. I have searched Google extensively with no avail so ANY help what so ever would be a brilliant help.
Thanks in advance!
EDIT: Uploaded my current treeview example

Right, I have a form with a Treeview in. I have it populated from a self-referencing database using the following code.
Code:
Private Sub Form_Load()
Const strTableQueryName = "SELECT * FROM tblHierarchy ORDER BY tblHierarchy.Function_Parent;"
Dim db As DAO.Database, rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset(strTableQueryName, dbOpenDynaset, dbReadOnly)
AddBranch rst, "Function_Parent", "Function_Id", "Function_Name"
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
' strNameField: Name of field containing text to be displayed
'=============================================================
Sub AddBranch(rst As Recordset, strPointerField As String, _
strIDField As String, strNameField As String, _
Optional varParentID 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(varParentID) Then ' Root Branch.
strCriteria = strPointerField & " Is Null"
Else ' Search for records pointing to parent.
strCriteria = BuildCriteria(strPointerField, _
rst.Fields(strPointerField).Type, "=" & varParentID)
Set nodParent = objTree.Nodes("a" & varParentID)
End If
' Find the first component to report to the rig node.
rst.FindFirst strCriteria
Do Until rst.NoMatch
' Create a string with Name
strText = rst(strIDField) & " - " & rst(strNameField)
strKey = "a" & rst(strIDField)
If Not IsMissing(varParentID) 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 components that belong to this node.
AddBranch rst, strPointerField, strIDField, strNameField, rst(strIDField)
rst.Bookmark = bk ' Return to last place and continue search.
rst.FindNext strCriteria ' Find next component.
Loop
exitAddBranch:
Exit Sub
'--------------------------Error Trapping --------------------------
errAddBranch:
MsgBox "Can't add child: " & Err.Description & " Key: " & strKey & " Text:" & strText, vbCritical, "AddBranch Error:"
Resume exitAddBranch
End Sub
The database this is referencing is about 30000 lines and it takes ~4 minutes to populate this way. I know Treeview isn't really supposed to be used in this way however it's what is required.
Now I have come up with the theory that I will populate each node with children as its clicked to be expanded. This in theory should drive down loading times as it's only populating what is being looked at, I however have no idea how to do this. I have searched Google extensively with no avail so ANY help what so ever would be a brilliant help.
Thanks in advance!
EDIT: Uploaded my current treeview example
Attachments
Last edited: