Thursday, February 14, 2008

My Treeview Project | Episode One: The Hello World! Treeview

The hello world program is a common first exercise when learning to work in a new programming environment: a program that simply displays or prints "Hello World!". In the first episode of this new series on treeviews we'll build the hello world treeview in an Access form.

Create a new blank Access database, create a new form and go into design view. Using Insert/Activex Control, place a Microsoft Treeview control on your form.

Size the control so you'll be able to see the branches we create; I made mine about 2" by 2". Name the control xMyTreeview.

Now go into the code editor and paste the following code as the form's On Load event.

Private Sub Form_Load()

' add a category node for messages
Me.xMyTreeview.Nodes.Add Text:="Messages", Key:="MsgCategory"
' add a single message to the messages category node
Me.xMyTreeview.Nodes.Add Relationship:=tvwChild, Relative:="MsgCategory", Text:="Hello World!"
End Sub
We'll look at the code soon, for now just paste it in.

Save the form, and then switch to form view mode.

You should see something like this:
Double click on "Messages" and it should expand to this:

There you have it- your first treeview in only two lines of code! Not actually useful yet, but it's a working treeview. In subsequent episodes we'll expand on this to make it useful. To finish up this episode we'll disect the two lines of code we've got so far.

Me.xMyTreeview.Nodes.Add Text:="Messages", Key:="MsgCategory"
This line adds the Messages node to the treeview. (Each row, or branch, of a treeview is called a node.) The Me.xMyTreeview just identifies the treeview we're working with; you called it xMyTreeview when you added it to the form. The treeview has a nodes collection- adding a node is accomplished using that collection's add method. In this case we've only used two of the Add method's parameters:

Pretty obvious- this specifies the text of the node. A treeview only displays one piece of data per node. If you want to use two pieces of data, such as a first and last name, you have to combine those together into a single string.

Each node of a treeview can have a key. You can use the key to identify the node when you need to refer to it later. We're giving this node the key MsgCategory, which we'll use to refer to this node when we add nodes that go under it (its children). Keys must be text. If you want to use a number you have to make it into a string first. This is because you can also refer to nodes using a numeric index, and you can't leave Access unclear whether you want to use the key or the index.

Me.xMyTreeview.Nodes.Add Relationship:=tvwChild, Relative:="MsgCategory", Text:="Hello World!"
This line adds a node with the text "Hello World!" and makes it a "child" of the messages node. It again uses the Add method of the Nodes collection. The text parameter works the same as it did last time. And we aren't specifying a key because we won't need to refer to this node later. But there are two new parameters:

This specifies that the node we're adding is a child of another node (the type of relationship it has to another node is a child relationship).

This specifies that the node the new node has a relationship to is the node with the key MsgCategory. The two parameters together tells Access to add this new node as a child of the node with the key MsgCategory (the node we added previously).

That's it for Episode 1! In epsiode 2 we build a treeview that shows data from two tables. Check out all the posts in MyTreeviewProject.

See also: other treeview posts on my blog.


Anonymous said...

Thank you so much for explaining the ActiveX Treeview Control in detail. You are a lifesaver!

Anonymous said...

Thanks for this, very helpful. sooooooooo much clearer now.

Anonymous said...

I have been trying to understand treeview controls for years. This is incredibly helpful. Thank you!