I've been working on getting this treeview to work and have issues adding the third child, and eventually need a fourth as well. The first parent and the next child load fine but won't load that childs, child (stack will be four deep on a parent). I'm working with four tables each linked with one-to-many.
Code:
Option Compare DatabaseOption Explicit
Dim rsCorp As DAO.Recordset
Dim rsInfoSec As DAO.Recordset
Dim rsFeature As DAO.Recordset
Dim rsControl As DAO.Recordset
Dim strSQL As String
Dim strFSQL As String
'----------------------
Private Sub Form_Load()
'----------------------
' set treeview control properties
With Me.treeView
' set style property to value that allows pictures
.Style = tvwTreelinesPlusMinusPictureText
' set to automatically expand selected node and collapse previous node
'.SingleSel = True ' Single Selection
End With
' initialize tree
TreeInit
End Sub
Private Sub TreeInit()
Dim trvTree As Control
Dim imgList As Control
Dim nodObject As Node
Dim i As Integer
Dim rcount As Integer
Dim strInfoSec As String
Dim strCorp As String
Dim strFeature As String
Dim strControl As String
Dim db As DAO.Database
' set database and recordset objects
Set db = CurrentDb
Set trvTree = Me.treeView
Set imgList = Me.MyImageList
' set Treeview control ImageList property to Image List Object
' treeview object style was previously set to 7-TvwTreelinesPlusMinusPictureText
' and the images were loaded manually into the imagelist control. Rather
' than use the image keys "frame" and "form", the images'
' index numbers are used when adding nodes.
trvTree.ImageList = imgList.Object
' use two recordsets, one for the parent and the other for the
' child. First create parent node with the linking field as
' a root node key. Next find the first and subsequent children
' matching the parent's linking field. Use the parent node's
' node key as the child node's relative value. When you run out
' of children, move to next parent record and then find its children,
' and so forth until there are no more parents. I supose this
' concept could be used with a third recordset to find children of
' the second recordset, and so on.
With trvTree.Nodes
' clear any nodes, if any
.Clear
' open needed recordsets
Set rsCorp = db.OpenRecordset("tblCorp", dbOpenSnapshot)
rsCorp.MoveLast
rcount = rsCorp.RecordCount
rsCorp.MoveFirst
Set rsInfoSec = db.OpenRecordset("tblInfoSec", dbOpenSnapshot)
Set rsFeature = db.OpenRecordset("tblFeatures", dbOpenSnapshot)
' loop through recordset and add nodes
While Not (rsCorp.EOF)
strCorp = rsCorp!Name
strInfoSec = rsInfoSec!IS_Name
strFeature = rsFeature!Description
' .. and add the parent node to Treeview
Set nodObject = .Add(, , "A" & CStr(rsCorp!Policy_ID), strCorp, 1)
strSQL = "Policy_ID = " & rsCorp!Policy_ID
strFSQL = "InfoSec_FK = " & rsInfoSec!InfoSec_PK
'find first child, if any, belonging to this parent
rsInfoSec.FindFirst strSQL
'on Error GoTo NoChild
' concatenate fields to create the child node text value.
While Not (rsInfoSec.NoMatch)
strInfoSec = rsInfoSec!IS_Name
' .. and add thee child node to the Treeview
Set nodObject = .Add("A" & rsCorp!Policy_ID, tvwChild, _
"C" & rsInfoSec!IS_Name, strInfoSec, 2)
' .. and create the node tag property holding key to child's baptism
trvTree.Nodes("C" & rsInfoSec!IS_Name).Tag = rsInfoSec!InfoSec_PK
' Now find next child, if any
rsFeature.FindFirst strFSQL
While Not (rsFeature.NoMatch)
strFeature = rsFeature!Description
Set nodObject = .Add("C" & rsInfoSec!IS_Name, tvwChild, _
"U" & rsFeature!Feature_PK, strFeature, 2)
trvTree.Nodes("U" & rsFeature!Feature_PK).Tag = rsFeature!Feature_PK
rsFeature.FindNext strFSQL
Wend
rsInfoSec.FindNext strSQL
NoFeature:
rsInfoSec.MoveNext
' .. and loop back and add next child
Wend
NoChild:
' no move children, so move to next parent
rsCorp.MoveNext
Wend
End With
Set rsCorp = Nothing
Set rsInfoSec = Nothing
Set rsFeature = Nothing
End Sub
Specifically, I get a Key is not unique error on this:
Code:
Set nodObject = .Add("C" & rsInfoSec!IS_Name, tvwChild, _
"U" & rsFeature!Feature_PK, strFeature, 2)
Any ideas where I went wrong or a better way to go about this? The key, "U" & rsFeature!Feature_PK is actually unique and thus confusing the hell out of me?!