I created a treeview control using two tables:software and version.



When I click in the tree on Software, it shows the nodes but it does not show the child nodes.

Option Compare Database
Option Explicit

'represents the tree view on the form
Private WithEvents tvw As MSComctlLib.TreeView

Private Sub Form_Open(Cancel As Integer)
'set a reference to the treeview that the control ctrlTreeview contains
'so intellisense is available on tvw
Set tvw = Me.ctrlTreeView.Object

End Sub

Private Sub Form_Load()
Dim sql As String

'add a root node
tvw.Nodes.Add , , "Root", "Software"

'create an SQL statement to drive the first level of node creation
'this is a recordset of reviewers, but rst fieldnames are changed
'to 'Key' and 'Text' so the node creation routine can be generic
'note that we prefix the key with an 's' to distinguish it from version keys
sql = "SELECT 's' & softwID as tKey, softwName as tText FROM tblSoftware;"

'call the create routine, passing in the sql as above, and the key of the parent node, in this case "Root"
CreateTree sql, "Root"


End Sub

Private Sub CreateTree(sql As String, parentKey As String)
Dim rst As DAO.Recordset
Dim nd As MSComctlLib.Node

'open an r-set, whose definition is not know at run-time
'but it must have the two fields: Key, and Text
Set rst = CurrentDb.OpenRecordset(sql)
With rst
'traverse all its records
Do While Not .EOF
'adding a node for every one
Set nd = tvw.Nodes.Add(parentKey, tvwChild, !tKey, !tText)
'if this is a software, then add version
If Left(!tKey, 1) = "r" Then
'make sure versions are visible
nd.EnsureVisible
'now add any child nodes of the current software by calling CreateTree with different parameters
'and in the WHERE clause, the prefixed character (s, in this case) is removed using Mid()
CreateTree "SELECT 'v' & verID as tKey, Description as tText FROM tblVersion WHERE softwID = " & Mid(!tKey, 2), !tKey
End If
'move to the next record
.MoveNext
Loop 'while not .eof
.Close 'rst
End With

End Sub