I am using a tree control (MSComctlLib.TreeCtrl.2) in a form...

B

billmahon

and want to be able to change the font color of items in the tree based on
certain criteria. Also, I would like to display information about the item
highlighted on the tree control onto another part of the form by passing the
value of the item on the tree control into the forms query. Can anyone help?
 
R

Rob Parker

You can set the color of a particular node via code such as:
nodx.ForeColor = vbRed 'using named colors
or
nodx.ForeColor = RGB(255,0,128) 'using rgb colors
or
nodx.ForeColor = 4194432 'using color code as in Access properties

Just put the statement into an If statement while you're loading the
treeview control to set the color depending on the criteria you want.

Note: For the examples above, I have the following statements in my code :
Dim nodx As Object
...
Set nodx = TreeView0.Nodes.Add([Relative], [Relationship], [Key],
[Text], [Image], [SelectedImage])
...

You can use the NodeClick event of the treeview control to get the Node.Key
and/or Node.Text and/or Node.Tag value of the node you select (either by
clicking on it, or by navigating though the tree with arrow keys). So, for
example, you could push the information into unbound textboxes on your form
with this code:

Private Sub TreeView0_NodeClick(ByVal Node As Object)
Me.txtNodeKey = Node.Key
Me.txtNodeTag = Node.Tag
Me.txtNodeText = Node.Text
End Sub

Or you could use the key/tag/text value directly in code to find a record in
the form's recordset; or use it to redefine the form's recordsource, or ...

A couple of tips:

Use the Object Browser in the VBA editor to check the properties and methods
available for the Node and Nodes classes, and the properties, methods and
events of the TreeView control.

To enter code into events of the treeview control, select the treeview
control in the object dropdown in the VBA editor, then use the Procedure
dropdown to select the event you want to code for.

HTH,

Rob
 
B

billmahon

Rob,

Thank you for your quick and helpful reply. I was able to readily adjust
the node colors based on the information you provided.

I am still struggling to pass the value of a node into the forms recordset.
Can you clarify your earlier suggestions?

Rob Parker said:
You can set the color of a particular node via code such as:
nodx.ForeColor = vbRed 'using named colors
or
nodx.ForeColor = RGB(255,0,128) 'using rgb colors
or
nodx.ForeColor = 4194432 'using color code as in Access properties

Just put the statement into an If statement while you're loading the
treeview control to set the color depending on the criteria you want.

Note: For the examples above, I have the following statements in my code :
Dim nodx As Object
...
Set nodx = TreeView0.Nodes.Add([Relative], [Relationship], [Key],
[Text], [Image], [SelectedImage])
...

You can use the NodeClick event of the treeview control to get the Node.Key
and/or Node.Text and/or Node.Tag value of the node you select (either by
clicking on it, or by navigating though the tree with arrow keys). So, for
example, you could push the information into unbound textboxes on your form
with this code:

Private Sub TreeView0_NodeClick(ByVal Node As Object)
Me.txtNodeKey = Node.Key
Me.txtNodeTag = Node.Tag
Me.txtNodeText = Node.Text
End Sub

Or you could use the key/tag/text value directly in code to find a record in
the form's recordset; or use it to redefine the form's recordsource, or ...

A couple of tips:

Use the Object Browser in the VBA editor to check the properties and methods
available for the Node and Nodes classes, and the properties, methods and
events of the TreeView control.

To enter code into events of the treeview control, select the treeview
control in the object dropdown in the VBA editor, then use the Procedure
dropdown to select the event you want to code for.

HTH,

Rob


billmahon said:
and want to be able to change the font color of items in the tree based on
certain criteria. Also, I would like to display information about the
item
highlighted on the tree control onto another part of the form by passing
the
value of the item on the tree control into the forms query. Can anyone
help?
 
R

Rob Parker

Hi Bill,

I don't understand exactly what you mean by "pass the value of a node into
the forms recordset". Could you explain exactly what it is that you're
trying to do?

Rob

billmahon said:
Rob,

Thank you for your quick and helpful reply. I was able to readily adjust
the node colors based on the information you provided.

I am still struggling to pass the value of a node into the forms
recordset.
Can you clarify your earlier suggestions?

Rob Parker said:
You can set the color of a particular node via code such as:
nodx.ForeColor = vbRed 'using named colors
or
nodx.ForeColor = RGB(255,0,128) 'using rgb colors
or
nodx.ForeColor = 4194432 'using color code as in Access properties

Just put the statement into an If statement while you're loading the
treeview control to set the color depending on the criteria you want.

Note: For the examples above, I have the following statements in my code
:
Dim nodx As Object
...
Set nodx = TreeView0.Nodes.Add([Relative], [Relationship], [Key],
[Text], [Image], [SelectedImage])
...

You can use the NodeClick event of the treeview control to get the
Node.Key
and/or Node.Text and/or Node.Tag value of the node you select (either by
clicking on it, or by navigating though the tree with arrow keys). So,
for
example, you could push the information into unbound textboxes on your
form
with this code:

Private Sub TreeView0_NodeClick(ByVal Node As Object)
Me.txtNodeKey = Node.Key
Me.txtNodeTag = Node.Tag
Me.txtNodeText = Node.Text
End Sub

Or you could use the key/tag/text value directly in code to find a record
in
the form's recordset; or use it to redefine the form's recordsource, or
...

A couple of tips:

Use the Object Browser in the VBA editor to check the properties and
methods
available for the Node and Nodes classes, and the properties, methods and
events of the TreeView control.

To enter code into events of the treeview control, select the treeview
control in the object dropdown in the VBA editor, then use the Procedure
dropdown to select the event you want to code for.

HTH,

Rob


billmahon said:
and want to be able to change the font color of items in the tree based
on
certain criteria. Also, I would like to display information about the
item
highlighted on the tree control onto another part of the form by
passing
the
value of the item on the tree control into the forms query. Can anyone
help?
 
B

billmahon

Yes, I am sorry.

I have a form into which I have inserted a tree control. The tree shows raw
materials under a Level 1 and level 2 category header. The window control
works fine.

Now, what I want to do is to display detail information on another part of
the form about a material that is clicked on in the tree control. The form
is based on a query containing information about each material.

So, in short, what I want to happen is when I select an item in the tree,
the value of that item (most likely the item name, which in your generic
example you referred to as "text") is pushed into the forms query, which then
filters the recordset, and displays detail information about that item on the
form.


I have attached the code I used to populate the tree control. The forms
query has a field called Suncode which would equal the sparentname.

Private Sub Form_Load()
Dim FormNode As Node
Dim rs As Object
Dim sParentId As String
Dim sProductId As String
Dim sProductName As String
Dim sProductGroupName, sProductGroupKey As String

With TreeView3
.Nodes.Clear
.Indentation = 2
.Style = 7
Set FormNode = .Nodes.Add(, , "Root", "Raw Materials " & "(" &
"Green are Core, Red are Non-Core" & ")")
FormNode.Expanded = True

Set rs = CurrentDb().OpenRecordset("select * from [tblrawmtlclass] order
by [RawMtlClassName]")

Do While Not rs.EOF
sProductGroupName = rs("RawMtlClassName")
sProductGroupKey = "g" & rs("RawMtlClassName")
Set FormNode = .Nodes.Add("Root", tvwChild, sProductGroupKey,
sProductGroupName)
FormNode.Expanded = False
rs.MoveNext
Loop
Set rs = CurrentDb().OpenRecordset("select * from [qryrawmtlcategories]
order by [rawmtlclassname]")
Do While Not rs.EOF
sProductName = rs("rawmtlnature")
sProductId = "p" & rs("categorycode")
sParentId = "g" & rs("RawMtlClassName")
Set FormNode = .Nodes.Add(sParentId, tvwChild, sProductId, sProductName)
FormNode.Expanded = False
rs.MoveNext
Loop

Set rs = CurrentDb().OpenRecordset("select * from [qrytestrawmtlusage]")
Do While Not rs.EOF
sProductName = rs("SunCode") & " (" & rs("commonname") & ")"
sProductId = "p" & rs("SunCode")
sParentId = "p" & rs("catid")
If sParentId = "0NA" Then sParentId = "g" & rs("catid")
Set FormNode = .Nodes.Add(sParentId, tvwChild, sProductId, sProductName)
FormNode.Expanded = True
If rs("coremtl?") = True Then
FormNode.ForeColor = 32768
FormNode.Bold = True
End If
If rs("coremtl?") = False Then
FormNode.ForeColor = vbRed
FormNode.Bold = False
End If


rs.MoveNext
Loop
End With

If Not rs Is Nothing Then rs.close
Set rs = Nothing


End Sub
I attempted to use your code. I placed an unbound text box into the form,
named it txtNodetext. Then I used the code you provided, and could not get
the value to show up in the text box.

Thanks in advance for your help.
 
R

Rob Parker

Hi Bill,

Got it now. And it's not hard.

First, the likely reason why you saw nothing in the unbound textbox after
clicking on a node is that you put the textbox in the detail section of the
form; and if you do that, then you'll need to requery the form to see the
change. If you put the unbound textbox (txtNodeText) in the form's header,
the new data (the node's text property) will show immediately you click on a
node. And that's what you need to do here, to get the following to work.
If you don't want to show this textbox in the forms header, you can set its
Visible property to no (and you can even size it so small that you can
collapse the form's header section to almost nothing, if you don't want the
header section to show either).

In the query which is your form's recordsource, add the following in the
criteria row for the field which you want to filter on:
[forms].[NameOfYourForm].[txtNodeText]

Then, all you need is the following code in the treeview control's NodeClick
event:

Private Sub TreeView0_NodeClick(ByVal Node As Object)
Me.txtNodeText = Node.Text
Requery
End Sub

And that's it!

I haven't really looked in any detail at the code you posted, since you say
it works fine. The only thing I did notice, in a quick skim, is that you
are using a numeric constant to colour nodes green, but a named
colorconstant (vbRed) to color nodes red. You could use vbGreen in a
similar fashion. If you want to see which colors you can use in that
manner, type "colorconstant" into the search box in the object browser -
there are 8 of them.

HTH,

Rob


billmahon said:
Yes, I am sorry.

I have a form into which I have inserted a tree control. The tree shows
raw
materials under a Level 1 and level 2 category header. The window control
works fine.

Now, what I want to do is to display detail information on another part of
the form about a material that is clicked on in the tree control. The
form
is based on a query containing information about each material.

So, in short, what I want to happen is when I select an item in the tree,
the value of that item (most likely the item name, which in your generic
example you referred to as "text") is pushed into the forms query, which
then
filters the recordset, and displays detail information about that item on
the
form.


I have attached the code I used to populate the tree control. The forms
query has a field called Suncode which would equal the sparentname.

Private Sub Form_Load()
Dim FormNode As Node
Dim rs As Object
Dim sParentId As String
Dim sProductId As String
Dim sProductName As String
Dim sProductGroupName, sProductGroupKey As String

With TreeView3
.Nodes.Clear
.Indentation = 2
.Style = 7
Set FormNode = .Nodes.Add(, , "Root", "Raw Materials " & "(" &
"Green are Core, Red are Non-Core" & ")")
FormNode.Expanded = True

Set rs = CurrentDb().OpenRecordset("select * from [tblrawmtlclass]
order
by [RawMtlClassName]")

Do While Not rs.EOF
sProductGroupName = rs("RawMtlClassName")
sProductGroupKey = "g" & rs("RawMtlClassName")
Set FormNode = .Nodes.Add("Root", tvwChild, sProductGroupKey,
sProductGroupName)
FormNode.Expanded = False
rs.MoveNext
Loop
Set rs = CurrentDb().OpenRecordset("select * from [qryrawmtlcategories]
order by [rawmtlclassname]")
Do While Not rs.EOF
sProductName = rs("rawmtlnature")
sProductId = "p" & rs("categorycode")
sParentId = "g" & rs("RawMtlClassName")
Set FormNode = .Nodes.Add(sParentId, tvwChild, sProductId,
sProductName)
FormNode.Expanded = False
rs.MoveNext
Loop

Set rs = CurrentDb().OpenRecordset("select * from
[qrytestrawmtlusage]")
Do While Not rs.EOF
sProductName = rs("SunCode") & " (" & rs("commonname") & ")"
sProductId = "p" & rs("SunCode")
sParentId = "p" & rs("catid")
If sParentId = "0NA" Then sParentId = "g" & rs("catid")
Set FormNode = .Nodes.Add(sParentId, tvwChild, sProductId,
sProductName)
FormNode.Expanded = True
If rs("coremtl?") = True Then
FormNode.ForeColor = 32768
FormNode.Bold = True
End If
If rs("coremtl?") = False Then
FormNode.ForeColor = vbRed
FormNode.Bold = False
End If


rs.MoveNext
Loop
End With

If Not rs Is Nothing Then rs.close
Set rs = Nothing


End Sub
I attempted to use your code. I placed an unbound text box into the form,
named it txtNodetext. Then I used the code you provided, and could not
get
the value to show up in the text box.

Thanks in advance for your help.


billmahon said:
and want to be able to change the font color of items in the tree based
on
certain criteria. Also, I would like to display information about the
item
highlighted on the tree control onto another part of the form by passing
the
value of the item on the tree control into the forms query. Can anyone
help?
 
B

billmahon

Worked like a charm. Thanks again for your help.

Rob Parker said:
Hi Bill,

Got it now. And it's not hard.

First, the likely reason why you saw nothing in the unbound textbox after
clicking on a node is that you put the textbox in the detail section of the
form; and if you do that, then you'll need to requery the form to see the
change. If you put the unbound textbox (txtNodeText) in the form's header,
the new data (the node's text property) will show immediately you click on a
node. And that's what you need to do here, to get the following to work.
If you don't want to show this textbox in the forms header, you can set its
Visible property to no (and you can even size it so small that you can
collapse the form's header section to almost nothing, if you don't want the
header section to show either).

In the query which is your form's recordsource, add the following in the
criteria row for the field which you want to filter on:
[forms].[NameOfYourForm].[txtNodeText]

Then, all you need is the following code in the treeview control's NodeClick
event:

Private Sub TreeView0_NodeClick(ByVal Node As Object)
Me.txtNodeText = Node.Text
Requery
End Sub

And that's it!

I haven't really looked in any detail at the code you posted, since you say
it works fine. The only thing I did notice, in a quick skim, is that you
are using a numeric constant to colour nodes green, but a named
colorconstant (vbRed) to color nodes red. You could use vbGreen in a
similar fashion. If you want to see which colors you can use in that
manner, type "colorconstant" into the search box in the object browser -
there are 8 of them.

HTH,

Rob


billmahon said:
Yes, I am sorry.

I have a form into which I have inserted a tree control. The tree shows
raw
materials under a Level 1 and level 2 category header. The window control
works fine.

Now, what I want to do is to display detail information on another part of
the form about a material that is clicked on in the tree control. The
form
is based on a query containing information about each material.

So, in short, what I want to happen is when I select an item in the tree,
the value of that item (most likely the item name, which in your generic
example you referred to as "text") is pushed into the forms query, which
then
filters the recordset, and displays detail information about that item on
the
form.


I have attached the code I used to populate the tree control. The forms
query has a field called Suncode which would equal the sparentname.

Private Sub Form_Load()
Dim FormNode As Node
Dim rs As Object
Dim sParentId As String
Dim sProductId As String
Dim sProductName As String
Dim sProductGroupName, sProductGroupKey As String

With TreeView3
.Nodes.Clear
.Indentation = 2
.Style = 7
Set FormNode = .Nodes.Add(, , "Root", "Raw Materials " & "(" &
"Green are Core, Red are Non-Core" & ")")
FormNode.Expanded = True

Set rs = CurrentDb().OpenRecordset("select * from [tblrawmtlclass]
order
by [RawMtlClassName]")

Do While Not rs.EOF
sProductGroupName = rs("RawMtlClassName")
sProductGroupKey = "g" & rs("RawMtlClassName")
Set FormNode = .Nodes.Add("Root", tvwChild, sProductGroupKey,
sProductGroupName)
FormNode.Expanded = False
rs.MoveNext
Loop
Set rs = CurrentDb().OpenRecordset("select * from [qryrawmtlcategories]
order by [rawmtlclassname]")
Do While Not rs.EOF
sProductName = rs("rawmtlnature")
sProductId = "p" & rs("categorycode")
sParentId = "g" & rs("RawMtlClassName")
Set FormNode = .Nodes.Add(sParentId, tvwChild, sProductId,
sProductName)
FormNode.Expanded = False
rs.MoveNext
Loop

Set rs = CurrentDb().OpenRecordset("select * from
[qrytestrawmtlusage]")
Do While Not rs.EOF
sProductName = rs("SunCode") & " (" & rs("commonname") & ")"
sProductId = "p" & rs("SunCode")
sParentId = "p" & rs("catid")
If sParentId = "0NA" Then sParentId = "g" & rs("catid")
Set FormNode = .Nodes.Add(sParentId, tvwChild, sProductId,
sProductName)
FormNode.Expanded = True
If rs("coremtl?") = True Then
FormNode.ForeColor = 32768
FormNode.Bold = True
End If
If rs("coremtl?") = False Then
FormNode.ForeColor = vbRed
FormNode.Bold = False
End If


rs.MoveNext
Loop
End With

If Not rs Is Nothing Then rs.close
Set rs = Nothing


End Sub
I attempted to use your code. I placed an unbound text box into the form,
named it txtNodetext. Then I used the code you provided, and could not
get
the value to show up in the text box.

Thanks in advance for your help.


billmahon said:
and want to be able to change the font color of items in the tree based
on
certain criteria. Also, I would like to display information about the
item
highlighted on the tree control onto another part of the form by passing
the
value of the item on the tree control into the forms query. Can anyone
help?
 
B

Brandon Baum

I just got an application that uses this object for maintenance.

When I upgrade the Access application to 2007 this object fails. Is there something I need to add as a reference or is this an object from earlier versions that was removed from 2007?
 
A

Arvin Meyer [MVP]

I just got an application that uses this object for maintenance.

When I upgrade the Access application to 2007 this object fails. Is there
something I need to add as a reference or is this an object from earlier
versions that was removed from 2007?

Probably removed, if you find a copy from an older machine, put it in your
Windows System32 directory. Register the file:

Start >>> Run

regsvr32.exe "C:\Windows\System32\comctl32.ocx"

Now set a reference to it in your Access application by opening any code
window and going to:

Tools >>> References

and selecting it.
 

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