Refresh a TreeView

A

aWs

I implemented a TreeView to my Bill Of Materials using the code sample in
http://support.microsoft.com/default.aspx?scid=kb;en-us;209891 (How to fill
a treeview....)

I have it attached to the Form_Load event.

I created a button to refresh the tree because in the form a user can add or
change the BOM and then want to see it updated.

How do I clear the TreeView object to rebuild it?

I thought I could just call the Sub again, but it errors, stating
duplicates. (I'm guessing because it is trying to rebuild without clearing.)
 
R

Ron Weiner

Put a

TreeViewControlName.Nodes.Clear

at the opt of your code that populates the tree
 
A

aWs

Thanks Ron,

Worked great!

If I may ask a follow up... Can the treeview be output to an excel file or
text file?
 
R

Ron Weiner

There are a number of methods that can be used to Populate a tree. Many
people will use a recursive algorithm and that is gonna be tougher to
organize into the desired out put. In most cases this is that easiest way
to write the code, but for large trees can be painfully slow to populate.
In this case you will likely have to write code that steps through each node
of the tree and either automate excel putting individual values into cells,
or write each node to a file that you could later import into excel.

When you know in advance how many levels deep the tree can ever be, you can
attempt to flatten out (relationally speaking) the trees data into one
(generally very nasty) query that could easily be exported to excel.
Typically this method of populating the tree is VERY fast as there is one
row in the query for each node and there is no recursing, but is a little
more complex to code.

Wow it just occurred to me that you might want the Graphic representation of
the tree, and no the data that the tree was based on. Frankly I have no
idea how you might accomplish that.

Ron W
www.WorksRite.com
 
A

aWs

Ron,

Your initial assumption is correct. I want to output the data not the
graphical representation. I've been exploring various articles on the
subject but it is the unknown levels that I keep getting hung up on.

Two more question about treeview.

1. After my treeview is created, only the highest value is visible and you
have to drill down to see the lower levels. Is there a way to expand or
explode all levels once the tree build is complete?
2.
3. After I implemented your original code suggestion I went back and changed
some things that I though would not make a difference. Note that
strTableQueryName is qry_BOM_Tree (the file my treeview is constructed from.)
What I did was placed a parameter in that query to select records matching
BOM_ID on frm_BOM_inq. Meaning in the criteria fields I added
[forms].[frm_BOM_inq].[BOM_ID]. If I run the query is works fine. But when
I click on my Tree Refresh botton it errors at line 6 stating Too Few
Parameters. Expected 1.

If I open the query and set the criteria manually and save it an press the
Refresh Button, that works.

How can I get the Forms BOM_ID to feed the qry_BOM_Tree so that When I build
the tree it is for the selected Product
CODE:
1 Private Sub btnRefreshTree_Click()

2 Const strTableQueryName = "qry_BOM_Tree"
3 Dim db As DAO.Database, rs As DAO.Recordset
4 Dim objTree As TreeView

5 Set db = CurrentDb
6 Set rs = db.OpenRecordset(strTableQueryName, dbOpenDynaset, dbReadOnly)
7 Set objTree = Me!xTree.Object

8 objTree.Nodes.Clear

9 AddBranch rs:=rs, strPointerField:="Parent_ID", strIDField:="Child_ID",
strTextField:="PartNumber", strDescField:="PartDesc", strQty:="Child_Qty"
End Sub
 
R

Ron Weiner

Assuming that the name of the tree control on your form is xTree, here are a
couple of subs that Collapse and Expand all of the nodes in the tree.

Private Sub cmdCollapse_Click()
' Purpose Collapse all nodes in the Tree
Dim i As Integer
If xTree.Nodes.Count > 0 Then
DoCmd.Hourglass True
For i = 1 To xTree.Nodes.Count
xTree.Nodes(i).Expanded = False
Next
DoCmd.Hourglass False
xTree.Nodes(1).EnsureVisible
xTree.Nodes(1).Selected = True
xTree.SetFocus
End If
End Sub

Private Sub cmdExpand_Click()
' Purpose Expand all nodes in the Tree
Dim i As Integer
If xTree.Nodes.Count > 0 Then
DoCmd.Hourglass True
For i = 1 To xTree.Nodes.Count
xTree.Nodes(i).Expanded = True
Next
DoCmd.Hourglass False
xTree.Nodes(1).EnsureVisible
xTree.Nodes(1).Selected = True
xTree.SetFocus
End If
End Sub


Try changing the Parameter [forms].[frm_BOM_inq].[BOM_ID] to
[forms]![frm_BOM_inq].[BOM_ID] and make sure that the form, frm_BOM_inq is
open when the recordset is opened. You can test to see what is going one
by stopping you code with a breakpoint at line 6 and in the Immediate window
(Ctrl+G) type ? [forms]![frm_BOM_inq].[BOM_ID] and hit enter. If the
reference is good then the value from that field will display on the next
line.

If that value is what you expected and you still get an error you need to
re-inspect your query qry_BOM_Tree.
--
Ron W
www.WorksRite.com
aWs said:
Ron,

Your initial assumption is correct. I want to output the data not the
graphical representation. I've been exploring various articles on the
subject but it is the unknown levels that I keep getting hung up on.

Two more question about treeview.

1. After my treeview is created, only the highest value is visible and you
have to drill down to see the lower levels. Is there a way to expand or
explode all levels once the tree build is complete?
2.
3. After I implemented your original code suggestion I went back and changed
some things that I though would not make a difference. Note that
strTableQueryName is qry_BOM_Tree (the file my treeview is constructed from.)
What I did was placed a parameter in that query to select records matching
BOM_ID on frm_BOM_inq. Meaning in the criteria fields I added
[forms].[frm_BOM_inq].[BOM_ID]. If I run the query is works fine. But when
I click on my Tree Refresh botton it errors at line 6 stating Too Few
Parameters. Expected 1.

If I open the query and set the criteria manually and save it an press the
Refresh Button, that works.

How can I get the Forms BOM_ID to feed the qry_BOM_Tree so that When I build
the tree it is for the selected Product
CODE:
1 Private Sub btnRefreshTree_Click()

2 Const strTableQueryName = "qry_BOM_Tree"
3 Dim db As DAO.Database, rs As DAO.Recordset
4 Dim objTree As TreeView

5 Set db = CurrentDb
6 Set rs = db.OpenRecordset(strTableQueryName, dbOpenDynaset, dbReadOnly)
7 Set objTree = Me!xTree.Object

8 objTree.Nodes.Clear

9 AddBranch rs:=rs, strPointerField:="Parent_ID", strIDField:="Child_ID",
strTextField:="PartNumber", strDescField:="PartDesc", strQty:="Child_Qty"
End Sub

Ron Weiner said:
There are a number of methods that can be used to Populate a tree. Many
people will use a recursive algorithm and that is gonna be tougher to
organize into the desired out put. In most cases this is that easiest way
to write the code, but for large trees can be painfully slow to populate.
In this case you will likely have to write code that steps through each node
of the tree and either automate excel putting individual values into cells,
or write each node to a file that you could later import into excel.

When you know in advance how many levels deep the tree can ever be, you can
attempt to flatten out (relationally speaking) the trees data into one
(generally very nasty) query that could easily be exported to excel.
Typically this method of populating the tree is VERY fast as there is one
row in the query for each node and there is no recursing, but is a little
more complex to code.

Wow it just occurred to me that you might want the Graphic representation of
the tree, and no the data that the tree was based on. Frankly I have no
idea how you might accomplish that.

Ron W
www.WorksRite.com

file
or sample
in (How
to can
add
 
Top