How to refresh a treeview on a form?

P

peterzeke

Hello: I'm new to using a treeview on a form.

I have a main form that contains a treeview. On the main form are two
subforms. One subform (sub A) displays data based on the selected node on the
tree. The second subform (sub B) displays any subordinates that are related
to the initial subform.

If I add additional records (i.e., subordinates) to the second subform (sub
B), how can I get the treeview to reflect the additional records (i.e.,
subordinates, nodes...) without having to close the entire form and then
reopen it?

All of the examples I come across on the internet show the treeview being
populated during the OnLoad event of the form, but nothing seems to exist for
refreshing a treeview when the underlying data changes (additions,deletions).

Any help on this will be greatly appreciated!

thanks,
Pete
 
G

Graham R Seach

It's worthwhile to note that the Clear method can sometimes be a very slow
process. It can also cause the vertical scrollbar to refresh multiple times
until the Nodes collection is empty. To resolve this issue, I turn off
repaints until it's all done. It's very fast!

Put the following into a standard module:

'Declarations section
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long,
lParam As Any) As Long

Public Const WM_SETREDRAW = &HB

Public Sub ClearTreeviewNodes(tvw As Treeview)
'Turn off node repaints
SendMessage tvw.hWnd, WM_SETREDRAW, 0, 0

'Clear the nodes collection
tvw.Nodes.Clear

'Turn on node repaints
SendMessage tvw.hWnd, WM_SETREDRAW, 1, 0
End Sub

Then, assuming your treeview is called, say "myTree", call the sub using the
following convention:
ClearTreeviewNodes Me!myTree.Object

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Office DevCon 2007. Sydney, Australia 3rd-4th Nov. Will you be there?
http://www.block.net.au/devcon/index.htm
 

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