Access Treeview Tutorial Part 3 – Refresh Treeview on Record Update


This blog post is about how to handle record updates so that the treeview is always showing accurate up-to date information. I have also included a few helper functions which will make coding with the treeview a bit easier.

The video

As usual I have created a youtube video which shows all steps required. Hope you will like it.

More explanation

This time I will try to also include at least part of the video content in the blog post as well. But for full value,  I strongly recommend watching the treeview, and/or downloading the Access database sample.

Updating the treeview

So we want to make sure that the information displayed in the treeview is accurate. This means that as a record is updated using our standard Access form, we want to also refresh the treeview. My usual approach is to simply reload the entire treeview. This works just fine if your treeview contains around 500-maybe 1000 records. If you use more then that amount of records in the treeview you might want to consider only partially populating the treeview, or having a way of selecting a subset of information before loading the treeview, or perhaps only refreshing the treeview instead of reloading it. 500-1000 records is what can be expected to load without a noticeable delay. More then that and the system might start to get a “sluggish” feel.

As written above, when the form gets updated we want to update the treeview. So in this case we use the forms AfterUpdate event to trigger the code. The video has more details and followup on logic, this post will simply contain the finished code:

Private Sub Form_AfterUpdate()
 Call  LoadTreeview
End Sub

Now this correctly causes the treeview to reload entirely, but now it can’t remember which node was the active one. So we need to tell the treeview to re-select the node which the form is currently standing on.

Private Sub Form_AfterUpdate()
  Call  LoadTreeview
  Dim tv as MSComctlLib.TreeView
  Set tv=Me.Parent.TreeReqs.Object
  tv.Nodes("<ID>" & me!PK_Req & "</ID>").Selected=True
End Sub

The node is now properly reselected after the treeview is updated. 🙂

Helper Functions

I want to have a easy way of referencing my treeview regardless of where I am in my code project. So I create a function which will return a treeview reference, namely THE reference to my treeview. Its quite simple as can be seen below:

Public Function GetTV() As MSComctlLib.TreeView
  Set GetTV=Forms("frm_Treeview_Example").TreeReqs.Object
End Function

The good part about using a helper function like this, is that as I type GetTv anywhere in my project, I now get to use Intellisense, so I can see all the properties, and methods of the treeview. Also should I wish to change to Latebound at a later point, I only have to change the single reference to make the GetTV return a object instead of a treeview. Note that this approach is slightly less effective then creating a local treeview variable. If you use it inside a loop while adding 1000 nodes for example I would recommend setting a local treeview variable and then setting that variable using the GetTV function. For example:

Dim tv as MScomctlLib.Treeview
  set tv=GetTv
For i =1 to 1000
Set tv=nothing

The last helper function is to make it easier writing the syntax for the node key. As you can see in Part 3 of my video series, as well as earlier parts, I use the syntax <ID>PrimaryKey</ID> as key for the node. So my helper function to convert the Primary Key to the Node key is:

Public Function GenReqNodeID(lngReqID as Long) as String
  GenReqNodeID="<ID>" & lngReqId & "</ID>"
End Function

If we take a look at the forms beforeUpdate event we can now rewrite it to look like so:

Private Sub Form_AfterUpdate()
  Call  LoadTreeview
End Su

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.