Results 1 to 8 of 8

Thread: Treeview (A2k)

  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Treeview (A2k)

    I use the following code behind my treeview form.
    The problem I have is with locating a name.
    The treeview works ok other than when clicking the 2nd node.
    In my table I hold the staff name and initial.
    Clicking the 2nd node changes details on a subform for that particular name, but !!
    If the surname is the same ie,
    Willett,K
    Willett,D
    The code doesn't determine the difference and only shows the first.
    Is there a way around this ?

    Option Compare Database
    Private Sub Form_Load()
    Dim db As Database
    Dim rst As Recordset
    Dim rstChild As Recordset
    Dim objNode As Node
    Dim strKey As String
    Dim strParent As String
    Set db = CurrentDb
    Set rst = db.OpenRecordset("tblDept")
    'Loop through tblDept and add each department
    'name to a node.
    With rst
    .MoveFirst
    If .RecordCount Then
    Do Until .EOF
    strKey = ![DeptID] & "L1"
    Set objNode = tvwTest.Nodes.Add(Key:=strKey, _
    Text:=![Department])
    objNode.Sorted = True
    .MoveNext
    Loop
    End If
    .Close
    End With
    Set rstChild = db.OpenRecordset("tblStaff")
    'Loop through tblEmployees and add each
    'employee to a node.
    With rstChild
    .MoveFirst
    If .RecordCount Then
    Do Until .EOF
    strKey = ![Code] & "L2"
    strParent = ![DeptID] & "L1"

    'identify the parent key for relative argument
    Set objNode = _
    tvwTest.Nodes.Add(relative:=strParent, _
    relationship:=tvwChild, Key:=strKey, _
    Text:=![Surname])
    .MoveNext
    Loop
    End If
    .Close
    End With
    db.Close
    Set rstChild = Nothing
    Set rst = Nothing
    Set db = Nothing
    End Sub

    Private Sub tvwTest_NodeClick(ByVal Node As Object)
    If Right$(Node.Key, 2) = "L2" Then txtLink = Mid$(Node.Text, InStr(Node.Text, " ") + 1)
    End Sub

  2. #2
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Treeview (A2k)

    From what I can see, your problem is that in you L2 nodes, you are making the text just the surname in this line:

    Set objNode = tvwTest.Nodes.Add(relative:=strParent, relationship:=tvwChild, Key:=strKey, Text:=![Surname])

    To get around this, it looks like you can use the Key and strip off the L2 from the end, since it look like the Key of the node is [CODE] & "L2"

    This will give you, what appears to be a unique key, and use that to link the sub form with
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  3. #3
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Treeview (A2k)

    Thanks for the comments.

    I'm still somewhat at a miss trying to understand whats really happening in the code and how it populates the tree.

    Would it be possible for some-one to breakdown and explain what happens in the chain of events.

    Sorry i've not got back in touch sooner, I've been away for the week.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Treeview (A2k)

    Dave,

    There are two loops that populate the treeview control. They use the Add method of tvwTest.Nodes. The Add method has several arguments:

    Add(relative, relationship, key, text, image, selectedimage)

    relationship can be used to indicate how the new node is to be placed compared to the node specified in relative, in particular to indicate that the new node is a child of the "parent" in relative.

    key can be used to specify a unique identifier for the new node; this is useful for later reference (for example, when creating child nodes).

    text is the text displayed in the tree view - this is what the user sees.

    image and selectedimage can be used to specify the icon to be displayed with the node; the first is the default icon, the second the icon to be displayed when the node has been selected (opened).

    In the OnLoad event procedure, the first loop

    Set rst = db.OpenRecordset("tblDept")
    'Loop through tblDept and add each department name to a node.
    With rst
    .MoveFirst
    If .RecordCount Then
    Do Until .EOF
    strKey = ![DeptID] & "L1"
    Set objNode = tvwTest.Nodes.Add(Key:=strKey, _
    Text:=![Department])
    objNode.Sorted = True
    .MoveNext
    Loop
    End If
    .Close
    End With

    loops through the records of tblDept and adds a node to the treeview for each record in tblDept. The relative and relationship arguments are not used here, so all nodes are added to the first level of the treeview. The key is set to the DeptID field concatenated with L1 to indicate that this is the first level, and the display text is set to the Department name. The Sorted property of the node is set to True; this ensures that the child nodes of this node (the employees in the department) will be displayed in alphabetical order.

    The second loop

    Set rstChild = db.OpenRecordset("tblStaff")
    'Loop through tblEmployees and add each
    'employee to a node.
    With rstChild
    .MoveFirst
    If .RecordCount Then
    Do Until .EOF
    strKey = ![Code] & "L2"
    strParent = ![DeptID] & "L1"
    'identify the parent key for relative argument
    Set objNode = _
    tvwTest.Nodes.Add(relative:=strParent, _
    relationship:=tvwChild, Key:=strKey, _
    Text:=![Surname])
    .MoveNext
    Loop
    End If
    .Close
    End With

    loops through all records of tblStaff and adds them to the second level of the treeview. In this loop, the relationship argument is set to tvwChild, to indicate that the new node is to be the child of a node on a higher level, and the relative argument is set to the key corresponding to the Department node: strParent = ![DeptID] & "L1". The display text is set to the surname only; if you want initials or first names or other info to be displayed, you should modify Text:=![Surname].

    Clear as mud now?

  5. #5
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Treeview (A2k)

    Clearer yes, thanks.

    As Mud ???

    Hans , yes this does make clearer sense now.
    Its a bit like a cascading combo scenario.
    Populate the first nodes, Populate the second nodes relating to Level-1, Populate a third relative to Level-2.

    I think I've made sense.
    Thanks again.

  6. #6
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Treeview (A2k)

    I've managed now to populate a third level.
    (See Code Atached)

    I have the [Code] twice as follows. Am I correct here with this or should they be of different relative values ?
    strKey = ![Code] & "L3"
    strParent = ![Code] & "L2"

    Also I can link the node_click to a subform via:

    Private Sub tvwTest_NodeClick(ByVal Node As Object)
    If Right$(Node.Key, 3) = "L3" Then txtLink = Mid$(Node.Text, InStr(Node.Text, " ") + 1)

    Which doesn't work any more. I assumed by changing the (Node.Key, ?) = "L?" to the Level 3 values, This would work.
    Obviously not.
    Attached Files Attached Files

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Treeview (A2k)

    Dave,

    You need to use Right$(Node.Key, 2) = "L3" in the NodeClick procedure, for you want to compare the rightmost 2 characters of the key to "L3", which is a string of length 2. The second argument of Right$ relates to the length, not to the level in the tree.

    By setting
    strKey = ![Code] & "L3"
    strParent = ![Code] & "L2"
    you are making each employee a child of himself/herself. Do you want to implement a multi-level hierarchical structure, i.e. Mary is manager of John, and John is manager of Ann, etc.? In that case, your table tblStaff should contain a field with the code of an employee's manager. You would use that field in strParent instead of the Code field, which relates to the employee himself/herself.

    If you have a structure with many levels, it would be more efficient to use a recursive procedure to populate the levels, in particular if the "depth" is not uniform (some departments have deeper subordinate levels than other departments). If you need this, I (or others) can post sample code for that.

  8. #8
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Treeview (A2k)

    Hans
    Thanks for the clarification. I'm just starting to study the TreeView in general, so some of the fundementals are a little strange.

    I welcome your code and advice, as they are good reference points.

    I've just dug up another treeview sample I think you sent me some months ago, where one Button is used to fill 3 levels and again a further three buttons to show how to fill each level individually.
    I hope to get a grasp of the TreeView control from all of this material.

    Have a good day

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •