S
Sam Guffey
Hello,
I'm having trouble getting a node to appear more than once
in a treeview control. In my senario a bill of material
may include the same node multiple times as long as the
parent item is different. My code however does not allow
this. This is my first attempt at using the treeview and
I'm at a loss as to how to correct my problem.
My data set is:
ItemNo ComNo Lvl
3174 5396 1
3174 3825 1
3174 5594 1
3174 5617 1
3174 5172 1
5172 4471 2
5172 4517 2
5594 5595 2
5594 5596 2
5617 4325 2
5617 4326 2
5596 5617 3
5617 4325 4
5617 4326 4
My code is:
Private Sub Form_Load()
On Error GoTo Error
Dim tvwBOM As Object
Dim lvwDetail As Object
Dim ilsImages As Object
Dim rst As DAO.Recordset
Dim db As DAO.Database
Dim mNodeParent As Node
Dim mNodeChild As Node
Set db = CurrentDb()
Set rst = db.OpenRecordset("PQ-PX0002")
Set tvwBOM = Me.TreeView
Set ilsImages = Me.ilsPicSm
tvwBOM.ImageList = ilsImages.Object
tvwBOM.Sorted = True
Application.Echo True
Do Until rst.EOF
Set mNodeParent = TreeView.Nodes("Key_" & rst
("ItemNo"))
Set mNodeChild = TreeView.Nodes.Add( _
Relative:=mNodeParent.index, _
Relationship:=tvwChild, _
Key:="Key_" & rst("ComNo"), _
Text:=rst("Child"))
With mNodeChild
'set other properties here
End With
rst.MoveNext
Loop 'through the recordset
'Clean up the treeview
For Each mNodeChild In TreeView.Nodes
mNodeChild.Expanded = True
Next
Application.Echo True
Error:
Select Case Err.Number
Case 35601 'node does not exist
Set mNodeParent = AddParentNode(Key:="Key_" &
rst("ItemNo"),
Text:=rst("Parent"))
Resume Next
Case 35602 'duplicated node key
Call AssignNewParent( _
Key:="Key_" & rst("ComNo"), _
ParentNodeIndex:=mNodeParent.index)
Resume Next
Case Else
Application.Echo True
If Err.Number <> 0 Then
MsgBox Err.Number & ": " & Err.Description
End If
End Select
End Sub
Private Function AddParentNode(Key As String, Text As
String) As Node
Dim mNodeParent As Node
On Error GoTo Error
Set mNodeParent = TreeView.Nodes.Add(, , Key,
Text, "ClsdFoldSm",
"OpenFoldSm") ' 13: Type mismatch
With mNodeParent
'set other propeties
End With
Set AddParentNode = mNodeParent
Exit Function
Error:
Application.Echo True
MsgBox Err.Number & ": " & Err.Description
End Function
Private Sub AssignNewParent(Key As String, ParentNodeIndex
As Integer)
On Error GoTo Error
Set TreeView.Nodes(Key).PARENT = TreeView.Nodes
(ParentNodeIndex)
Exit Sub
Error:
Application.Echo True
MsgBox Err.Number & ": " & Err.Description
End Sub
My results should be:
3174
5396
3825
5594
5595
5596
5617
4325
4326
5172
4471
4517
5617
4325
4326
My results as the code is written are:
3174
5396
3825
5594
5595
5596
5617
4325
4326
5172
4471
4517
If I concatenate ComNo and level (Lvl) in order to get a
unique key for 5617 my results become:
5594
5595
5596
5596
5617
3174
5396
3825
5594
5617
5172
5617
4325
4326
4325
4326
5172
4471
4517
Any help would be greatly appreciated!
Thanks
Sam Guffey
I'm having trouble getting a node to appear more than once
in a treeview control. In my senario a bill of material
may include the same node multiple times as long as the
parent item is different. My code however does not allow
this. This is my first attempt at using the treeview and
I'm at a loss as to how to correct my problem.
My data set is:
ItemNo ComNo Lvl
3174 5396 1
3174 3825 1
3174 5594 1
3174 5617 1
3174 5172 1
5172 4471 2
5172 4517 2
5594 5595 2
5594 5596 2
5617 4325 2
5617 4326 2
5596 5617 3
5617 4325 4
5617 4326 4
My code is:
Private Sub Form_Load()
On Error GoTo Error
Dim tvwBOM As Object
Dim lvwDetail As Object
Dim ilsImages As Object
Dim rst As DAO.Recordset
Dim db As DAO.Database
Dim mNodeParent As Node
Dim mNodeChild As Node
Set db = CurrentDb()
Set rst = db.OpenRecordset("PQ-PX0002")
Set tvwBOM = Me.TreeView
Set ilsImages = Me.ilsPicSm
tvwBOM.ImageList = ilsImages.Object
tvwBOM.Sorted = True
Application.Echo True
Do Until rst.EOF
Set mNodeParent = TreeView.Nodes("Key_" & rst
("ItemNo"))
Set mNodeChild = TreeView.Nodes.Add( _
Relative:=mNodeParent.index, _
Relationship:=tvwChild, _
Key:="Key_" & rst("ComNo"), _
Text:=rst("Child"))
With mNodeChild
'set other properties here
End With
rst.MoveNext
Loop 'through the recordset
'Clean up the treeview
For Each mNodeChild In TreeView.Nodes
mNodeChild.Expanded = True
Next
Application.Echo True
Error:
Select Case Err.Number
Case 35601 'node does not exist
Set mNodeParent = AddParentNode(Key:="Key_" &
rst("ItemNo"),
Text:=rst("Parent"))
Resume Next
Case 35602 'duplicated node key
Call AssignNewParent( _
Key:="Key_" & rst("ComNo"), _
ParentNodeIndex:=mNodeParent.index)
Resume Next
Case Else
Application.Echo True
If Err.Number <> 0 Then
MsgBox Err.Number & ": " & Err.Description
End If
End Select
End Sub
Private Function AddParentNode(Key As String, Text As
String) As Node
Dim mNodeParent As Node
On Error GoTo Error
Set mNodeParent = TreeView.Nodes.Add(, , Key,
Text, "ClsdFoldSm",
"OpenFoldSm") ' 13: Type mismatch
With mNodeParent
'set other propeties
End With
Set AddParentNode = mNodeParent
Exit Function
Error:
Application.Echo True
MsgBox Err.Number & ": " & Err.Description
End Function
Private Sub AssignNewParent(Key As String, ParentNodeIndex
As Integer)
On Error GoTo Error
Set TreeView.Nodes(Key).PARENT = TreeView.Nodes
(ParentNodeIndex)
Exit Sub
Error:
Application.Echo True
MsgBox Err.Number & ": " & Err.Description
End Sub
My results should be:
3174
5396
3825
5594
5595
5596
5617
4325
4326
5172
4471
4517
5617
4325
4326
My results as the code is written are:
3174
5396
3825
5594
5595
5596
5617
4325
4326
5172
4471
4517
If I concatenate ComNo and level (Lvl) in order to get a
unique key for 5617 my results become:
5594
5595
5596
5596
5617
3174
5396
3825
5594
5617
5172
5617
4325
4326
4325
4326
5172
4471
4517
Any help would be greatly appreciated!
Thanks
Sam Guffey