Wrox Programmer Forums
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 28th, 2005, 09:54 AM
Friend of Wrox
Join Date: Jan 2005
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Default MSTree View - Sync problem

I am using the MS tree view ocx to allow users to navigate through a master detail situation. I have no problems loading the control and syncing the form recordset to the selected item in the tree view when an item is clicked. What I want to do is sync the tree view control to the selected record so when the on current event fires it finds the related item in the tree view control. The ocx does not have a method for this. and so I need an alternative. Any suggesstions are welcome!!

Old January 30th, 2005, 08:17 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts

Hi Jp,

This will select a node in the treeview that corresponds to the current record on the Main form (I'm assuming there is only one form involved, i.e. no subform), expand any parent nodes the selected node may have, and highlight the selected node. Be sure the "HideSelection" property of the ocx is set to 'No' ('Yes' is the default).

Private Sub Form_Current()

    On Error GoTo ErrorHandler

    ' Get a refernece to the TreeView control
    ' on the Main form.
    Set oTreeView = Me!TreeView1

    If Not oTreeView Is Nothing Then

        Dim strKey As String
        strKey = Me!ItemText

        Set oNode = oTreeView.Nodes(strKey)
        oTreeView.SelectedItem = oNode

    End If

    Exit Sub

    If Err.Number = 35601 Then 'Element not found
        MsgBox "There is no Node key corresponding to the current record."
        Exit Sub
        MsgBox Err.Source & "-->" & Err.Description, , "Error"
    End If
End Sub

To make this work, you have to reference the nodes collection by node key, which mean the node key has to be set as you add each node to the nodes collection. I'm basing the Main form's recordset on a single table with some real simple data like:
"ItemID"    "ItemText"
1            "Parent1"
2            "Parent2"
3            "Parent3"
4            "Child1"
5            "Child2"
6            "Child3"
7            "Child4"
8            "Child5"
9            "Child6"
So as each node is added to the nodes collection, its key gets set to a corresponding data element, like:

Private Sub Form_Load()
    Dim oNode As Node
    Dim i As Integer

    'Add method arguments
    'object.Add(relative, relationship, key, text, image, selectedimage)

    For i = 1 To 3
        Set oNode = TreeView1.Nodes.Add(, , "Parent" & CStr(i), "Parent" & CStr(i))
    Next i

    Set oNode = TreeView1.Nodes.Add(1, 4, "Child1", "Child1")
    Set oNode = TreeView1.Nodes.Add(1, 4, "Child2", "Child2")
    Set oNode = TreeView1.Nodes.Add(2, 4, "Child3", "Child3")
    Set oNode = TreeView1.Nodes.Add(2, 4, "Child4", "Child4")
    Set oNode = TreeView1.Nodes.Add(3, 4, "Child5", "Child5")
    Set oNode = TreeView1.Nodes.Add(3, 4, "Child6", "Child6")

End Sub

I hard coded this stuff just to keep it simple, but you're probably using some sort of loop to populate your treeview from a recordset, in which case just to something like:

Set oNode = TreeView1.Nodes.Add()
With oNode
    .Key = "Key_" & rst("ItemText") 'or ItemID, which ever you prefer
    .Text = rst("ItemText")
End With

Once the key is set, you should be good to go.



Old January 31st, 2005, 04:34 AM
Friend of Wrox
Join Date: Jan 2005
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks Bob!

I didn't think of setting a reference to the node! I was thinking of some sort of 'Find First'!!! (Doh!)

Pefect, the db is doing exactly what I wanted. Thanks alot!!!:D
Old January 31st, 2005, 06:19 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts

My pleasure JpJoe! Glad its working OK for you. :)


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sync FormView and DataList CB40 ASP.NET 2.0 Basics 1 January 26th, 2008 10:43 PM
Database Sync Rangatharan .NET Framework 2.0 1 August 20th, 2007 02:37 AM
asynch vs. sync hdwriter BOOK: Professional Ajax ISBN: 978-0-471-77778-6 1 April 11th, 2007 10:22 PM
MySQL Sync rajuru MySQL 2 November 9th, 2006 10:08 AM
A Better way to sync a remote and local database? kevorkian SQL Server 2000 1 April 5th, 2005 08:09 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.