Need help with first TreeView control

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
 
G

Guest

Hi sam,

I marked your problems.

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"), _ <----- Here's your problem
Text:=rst("Child"))
With mNodeChild
'set other properties here
End With

The key of the node must be unique in ALL the tree, try using other parameters (like a key field in the source table) and it would work.
 
G

Guest

Hi Daniel,

I tried your suggestion and my result set turned out the
same as when I tried to concatenate ComNo and Lvl. All of
the data is present but the tree looses a lot of the
parent/child relationships. My results are:

5594
5595
5596
5596
5617
3174
5396
3825
5594
5617
5172
5617
4325
4326
4325
4326
5172
4471
4517

When they should be:

3174
5396
3825
5594
5595
5596
5617
4325
4326
5172
4471
4517
5617
4325
4326

I agree with your assessment of my problem. Everything
I've read says that I must have a unique key, however if
this is true then I must also have a second issue that I'm
unaware of.

I appreciate your assistance with this problem.

Thanks!
Sam
-----Original Message-----
Hi sam,

I marked your problems.

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"), _ <-- --- Here's your problem
Text:=rst("Child"))
With mNodeChild
'set other properties here
End With

The key of the node must be unique in ALL the tree, try
using other parameters (like a key field in the source
table) and it would work.
 
G

Guest

The use of "Lvl & ComNo" as a key is also wrong, there could be duplicate values also (think about it).
As for the tree itself - try to build it recursively, it's much simpler.
On the begining of each function - (Enter with a parentNode) query the database regarding the chiled's of this node and add them recursively.

about the levels of the tree you got, the loop you wrote doesn't go more than 2 levels deep in the tree, so try to implement the solution above and youre code would be correct to build the tree you want.

good luck

Daniel
 
S

Sam Guffey

Thanks Daniel. I'll give this a shot and let you know.

Sam


Daniel said:
The use of "Lvl & ComNo" as a key is also wrong, there could be duplicate values also (think about it).
As for the tree itself - try to build it recursively, it's much simpler.
On the begining of each function - (Enter with a parentNode) query the
database regarding the chiled's of this node and add them recursively.
about the levels of the tree you got, the loop you wrote doesn't go more
than 2 levels deep in the tree, so try to implement the solution above and
youre code would be correct to build the tree you want.
 
S

Sam Guffey

Daniel,
You were correct, recursion was the key. I've got it working now. Thanks
again for all of your help!

Sam

Daniel said:
The use of "Lvl & ComNo" as a key is also wrong, there could be duplicate values also (think about it).
As for the tree itself - try to build it recursively, it's much simpler.
On the begining of each function - (Enter with a parentNode) query the
database regarding the chiled's of this node and add them recursively.
about the levels of the tree you got, the loop you wrote doesn't go more
than 2 levels deep in the tree, so try to implement the solution above and
youre code would be correct to build the tree you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top