Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TreeView click 3

Status
Not open for further replies.

ryan1

Programmer
May 15, 2002
106
US
Here's basically what I'm trying to do. I have a treeview that I have filled. The treeview is located on a form with another subform when I click on the 1st node I want the record on the subform to reflect this. Sort of like a relationship you would see with a one to many. Any ideas would be appreciated.
 
ryan:

How I accomplished this was when I was filling the tree, I placed the primary key of each record (hence, each treenode is unique) into the nodekey of each treenode. Then when the user clicks on a node, you simply read the primary key from the nodekey, and load the correct record into your subform.

HTH



Greg Tammi, IT Design & Consultation
Work: Home:
 
grtammi: Where do you get your activex information? Are there tutorials out there for using them within Access? All my searches either turn up VB or ADO.NET programming, but I want to see the use in Access.

I have found to be a site that deals much heavier with treeview controls, especially the UI Design forum. But I still prefer tek-tips for answers to problems:) The site is set up for attachments, and I have gotten probably 4 or 5 treeview examples from there.

Sean.
 
perrymans:

I'm not sure where I figure this stuff out from, but here's a piece of example code that I use for my Treeview control (there's a blurb about halfway down on how to use the Treeview control):

Code:
Public Sub WriteTree()

On Error GoTo Tree_Error

' *********************************************************
' subroutine writes out telephone info for the customers
' *********************************************************
' ADO variable declarations and sets
    Dim conn As ADODB.Connection
    Set conn = CurrentProject.Connection
    Dim rs As ADODB.Recordset
    Set rs = CreateObject("ADODB.Recordset")
' miscellaneous variable declarations
    Dim tSQL As String: tSQL = ""
    Dim treSQL As String
    Dim sKeyString As String
    Dim sDisplay As String
' -----------------------------------------------
' set up SQL string 
    tSQL = SELECT DISTINCT id, phone_num, cName FROM " _
       & "tblLLFeature WHERE trans_date BETWEEN #" _
       & dteStart & "# AND #" & dteEnd & "#;"
' -----------------------------------------------
' clear existing tree nodes
    Me!treCust.Nodes.Clear
' -----------------------------------------------
' open recordset
    rs.Open tSQL, conn
' *********************************************************
' the add method used below utilizes the following syntax
' <treeview control name>.Nodes.Add 1, 2, 3, 4 where:
' 1 = identifying key node back to the parent (relates to #3 of parent node); if parent, leave blank
' 2 = type of node being added ; blank = parent, tvwChild = child node.
' 3 = unique key name for this node - usually use primary key of recordset
' 4 = text description of node (what user sees)
' I haven't yet found anything good to describe this tree node thingy, so this
' is what I've come up with - seems to work
' -----------------------------------------------
' write parent nodes out to tree, using the phone number as the key value,
' and display the customer phone number and name as node text
    While Not rs.EOF
        ' set node key string
        sKeyString = CStr(rs(1))
        ' set node display text
        sDisplay = rs(1) & &quot; :: [ &quot; & rs(&quot;cName&quot;) & &quot; ]&quot;
        ' add node
        Me!treCust.Nodes.Add , , sKeyString, sDisplay
        rs.MoveNext
    Wend
    rs.Close
' -----------------------------------------------
' get all feature transactions for each customer, 
' and write to the tree (prefix: LLF)
  treSQL = &quot;SELECT id, trans_date, salesperson, &quot; _
    & &quot;feature, order_num, ATScomm, phone_num, &quot; _
    & &quot;ATSStatus, recNum FROM tblLLFeature WHERE &quot; _
    & &quot;trans_date BETWEEN #&quot; & dteStart & &quot;# AND #&quot; _
    & dteEnd & &quot;#;&quot;
  ' open recordset
  rs.Open treSQL, conn
  ' iterate recordset
  While Not rs.EOF
    ' set node key string
    sKeyString = &quot;LLF,&quot; & rs(6) & &quot;,&quot; & rs(0) & &quot;,&quot; _
      & rs(1) & &quot;,&quot; & rs(2) & &quot;,&quot; & rs(3) & &quot;,&quot; _
      & rs(5) & &quot;,&quot; & rs(8)
    ' set node display text
    sDisplay = &quot;LLF: &quot; & rs(1) & &quot; -> &quot; & rs(3) _
      & &quot; for: $&quot; & CCur(rs(5))
    ' add node to tree
    Me!treCust.Nodes.Add CStr(rs(6)), tvwChild, sKeyString, sDisplay
    ' move to next record
    rs.MoveNext
  Wend
  rs.Close
End If
        
Tree_Exit:
    ' hourglass off
    DoCmd.Hourglass False
    ' free up memory by releasing variables
    tSQL = &quot;&quot;: treSQL = &quot;&quot;: sDisplay = &quot;&quot;: sKeyString = &quot;&quot;
    Exit Sub

Tree_Error:
    Select Case Err.Number
        Case 35601  ' element not found error
            Resume Next
        Case 35602  ' not unique key
            Resume Next
        Case Else
            MsgBox &quot;Error Number: &quot; & Err.Number & vbCrLf & &quot;Description: &quot; & Err.Description & vbCrLf & vbCrLf _
                & &quot;Contact your database administrator for further assistance.&quot;, vbCritical, &quot;STOP!&quot;
    End Select
    
    DoCmd.Hourglass False
    
End Sub


like I said, this is just a sample piece of code from my Treeview control, but I hope it gives you an idea on how to use it ... maybe I should this as an FAQ?


Greg Tammi, IT Design & Consultation
Work: [URL unfurl="true"]http://www.atsbell.com[/URL]
Home: [URL unfurl="true"]http://members.shaw.ca/gregandliz[/URL]
 
perrymans:

I'm not sure where I figure this stuff out from, but here's a piece of example code that I use for my Treeview control (there's a blurb about halfway down on how to use the Treeview control):

Code:
Public Sub WriteTree()

On Error GoTo Tree_Error

' *********************************************************
' subroutine writes out telephone info for the customers
' *********************************************************
' ADO variable declarations and sets
    Dim conn As ADODB.Connection
    Set conn = CurrentProject.Connection
    Dim rs As ADODB.Recordset
    Set rs = CreateObject(&quot;ADODB.Recordset&quot;)
' miscellaneous variable declarations
    Dim tSQL As String: tSQL = &quot;&quot;
    Dim treSQL As String
    Dim sKeyString As String
    Dim sDisplay As String
' -----------------------------------------------
' set up SQL string 
    tSQL = SELECT DISTINCT id, phone_num, cName FROM &quot; _
       & &quot;tblLLFeature WHERE trans_date BETWEEN #&quot; _
       & dteStart & &quot;# AND #&quot; & dteEnd & &quot;#;&quot;
' -----------------------------------------------
' clear existing tree nodes
    Me!treCust.Nodes.Clear
' -----------------------------------------------
' open recordset
    rs.Open tSQL, conn
' *********************************************************
' the add method used below utilizes the following syntax
' <treeview control name>.Nodes.Add 1, 2, 3, 4 where:
' 1 = identifying key node back to the parent (relates to #3 of parent node); if parent, leave blank
' 2 = type of node being added ; blank = parent, tvwChild = child node.
' 3 = unique key name for this node - usually use primary key of recordset
' 4 = text description of node (what user sees)
' I haven't yet found anything good to describe this tree node thingy, so this
' is what I've come up with - seems to work
' -----------------------------------------------
' write parent nodes out to tree, using the phone number as the key value,
' and display the customer phone number and name as node text
    While Not rs.EOF
        ' set node key string
        sKeyString = CStr(rs(1))
        ' set node display text
        sDisplay = rs(1) & &quot; :: [ &quot; & rs(&quot;cName&quot;) & &quot; ]&quot;
        ' add node
        Me!treCust.Nodes.Add , , sKeyString, sDisplay
        rs.MoveNext
    Wend
    rs.Close
' -----------------------------------------------
' get all feature transactions for each customer, 
' and write to the tree (prefix: LLF)
  treSQL = &quot;SELECT id, trans_date, salesperson, &quot; _
    & &quot;feature, order_num, ATScomm, phone_num, &quot; _
    & &quot;ATSStatus, recNum FROM tblLLFeature WHERE &quot; _
    & &quot;trans_date BETWEEN #&quot; & dteStart & &quot;# AND #&quot; _
    & dteEnd & &quot;#;&quot;
  ' open recordset
  rs.Open treSQL, conn
  ' iterate recordset
  While Not rs.EOF
    ' set node key string
    sKeyString = &quot;LLF,&quot; & rs(6) & &quot;,&quot; & rs(0) & &quot;,&quot; _
      & rs(1) & &quot;,&quot; & rs(2) & &quot;,&quot; & rs(3) & &quot;,&quot; _
      & rs(5) & &quot;,&quot; & rs(8)
    ' set node display text
    sDisplay = &quot;LLF: &quot; & rs(1) & &quot; -> &quot; & rs(3) _
      & &quot; for: $&quot; & CCur(rs(5))
    ' add node to tree
    Me!treCust.Nodes.Add CStr(rs(6)), tvwChild, sKeyString, sDisplay
    ' move to next record
    rs.MoveNext
  Wend
  rs.Close
End If
        
Tree_Exit:
    ' hourglass off
    DoCmd.Hourglass False
    ' free up memory by releasing variables
    tSQL = &quot;&quot;: treSQL = &quot;&quot;: sDisplay = &quot;&quot;: sKeyString = &quot;&quot;
    Exit Sub

Tree_Error:
    Select Case Err.Number
        Case 35601  ' element not found error
            Resume Next
        Case 35602  ' not unique key
            Resume Next
        Case Else
            MsgBox &quot;Error Number: &quot; & Err.Number & vbCrLf & &quot;Description: &quot; & Err.Description & vbCrLf & vbCrLf _
                & &quot;Contact your database administrator for further assistance.&quot;, vbCritical, &quot;STOP!&quot;
    End Select
    
    DoCmd.Hourglass False
    
End Sub


like I said, this is just a sample piece of code from my Treeview control, but I hope it gives you an idea on how to use it ... maybe I should write this as an FAQ?


Greg Tammi, IT Design & Consultation
Work: [URL unfurl="true"]http://www.atsbell.com[/URL]
Home: [URL unfurl="true"]http://members.shaw.ca/gregandliz[/URL]
 
Thanks for the code snippet, * 4 U.

I really want a resource to learn more though. Take imageLists for instance. How they correlate with TreeView's is totally foreign to me, and i can't seem to find indications of how to use them together (though I have completed examples of them).

I it all the same as in VB? Should I just look into VB explainations rather than Access specific ones?

Thanks. Sean.
 
Dear Perrymans,

VB Manuals are great places to get code samples.

Here is a link to MSDN for the TreeView control(and other controls):

Then Navigate to:
Visual Tools
Visual Studio 6.0
Visual Basic 6.0
Product Documentation
Using Visual Basic
Components Tools Guide
Using Active X Controls
(finally, pick the control you need)

Microsoft has it all, but it certainly is tough to find it!!

Hope This Helps,
Hap [2thumbsup]

Access Developer [pc] Access based Add-on Solutions
Access Consultants forum
 
I'm still trying to figure out how to click on a node and updating a subform on the same form?
 
ryan:

here's some code that I use to get the value of the text of the selected tree node :
Code:
Private Sub YourTreeViewControl_DblClick()
    
   Dim currNode As Node, nodeText As String
   Set currNode = Me!treCust.SelectedItem
   nodeText = currNode.Text

   MsgBox &quot;You clicked on &quot; & nodeText

End Sub

that should get you started ...


Greg Tammi, IT Design & Consultation
Work: Home:
 
Thanks, i managed to get this far, but unsure how to update the subform.
 
you say you have a &quot;one-to-many&quot; type relationship on your subform ... then what I'd do is once I've got the value from the treeview control (the one-side), I'd either filter my subform with that value, or building a new SQL statement and reset my subform data source:

...
set ctl = forms!MyMainForm!MySubFormControl
ctl.Form!RecordSource = &quot;SELECT * FROM MyDataTable WHERE MyRestrictedField=&quot; & TreeViewTextValue & &quot;;&quot;
...

Of course, you'll have to make the mods where appropriate, but that should get you a little further along.


Greg Tammi, IT Design & Consultation
Work: Home:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top