Treeview control - getting selected node value from recordset

TR21Mark

Board Regular
Joined
Oct 30, 2004
Messages
240
I have downloaded this snippet from MSDN library. I have a table with 3 fields and a query for the table. The fields are

'qryTVNode' (name of query

TVNID (Autonumber PK ID)
TVNName (Treeview Node Name)
TVNparID (field holds the parent ID it is related to)

I have the treeview populated and everything is working great. Now on to the next step. I need to get the value of the TNNparID of the selected node. I have a textbox on this form txtSelNode that I would like this value placed after selection.

In review of the code, strPointerField is the field in the recordset that would hold the value I need. Can someone review and steer me in the right direction?

Thanks to all in advance

Code:
Option Compare Database
Option Explicit

Private Sub Form_Load()

'Downloaded from MS MSDN library

Const strTableQueryName = "qryTVNodes"
      Dim db As Database, rst As Recordset
      Set db = CurrentDb
      Set rst = db.OpenRecordset(strTableQueryName, dbOpenDynaset, _
      dbReadOnly)
      AddBranch _
         rst:=rst, _
         strPointerField:="TVNparID", _
         strIDField:="TVNID", _
         strTextField:="TVNName"
      End Sub

      '================= AddBranch Sub Procedure =========================
      '      Recursive Procedure to add branches to TreeView Control
      'Requires:
      '   ActiveX Control:  TreeView Control
      '              Name:  xTree
      'Parameters:
      '               rst:  Self-referencing Recordset containing the data
      '   strPointerField:  Name of field pointing to parent's primary key
      '        strIDField:  Name of parent's primary key field
      '      strTextField:  Name of field containing text to be displayed
      '===================================================================
      Sub AddBranch(rst As Recordset, strPointerField As String, _
                    strIDField As String, strTextField As String, _
                    Optional varReportToID As Variant)
      On Error GoTo errAddBranch
      Dim nodCurrent As Node, objTree As TreeView
      Dim strCriteria As String, strText As String, strKey As String
      Dim nodParent As Node, bk As String
      Set objTree = Me!xTree.Object
      If IsMissing(varReportToID) Then  ' Root Branch.
         strCriteria = strPointerField & " Is Null"
      Else  ' Search for records pointing to parent.
         strCriteria = BuildCriteria(strPointerField, _
         rst.Fields(strPointerField).Type, _
         "=" & varReportToID)
         Set nodParent = objTree.Nodes("a" & varReportToID)
      End If
      ' Find the first
      rst.FindFirst strCriteria
      Do Until rst.NoMatch
         ' Create a string
         strText = rst(strTextField)
         strKey = "a" & rst(strIDField)
         If Not IsMissing(varReportToID) Then  'add new node to the parent
            Set nodCurrent = objTree.Nodes.Add(nodParent, _
            tvwChild, strKey, strText)
         Else    ' Add new node to the root.
            Set nodCurrent = objTree.Nodes.Add(, , strKey, _
            strText)
         End If
         ' Save your place in the recordset so we can pass by ref for
         ' speed.
         bk = rst.Bookmark
         ' Add  to this node.
         AddBranch rst, strPointerField, strIDField, strTextField, _
         rst(strIDField)
         rst.Bookmark = bk     ' Return to last place and continue search.
         rst.FindNext strCriteria   ' Find next
         
      
         
      Loop
exitAddBranch:
      Exit Sub
      '--------------------------Error Trapping --------------------------
errAddBranch:
      MsgBox "Can't add child:  " & Err.Description, vbCritical, _
      "AddBranch Error:"
      Resume exitAddBranch
      End Sub
                    
Private Sub xTree_NodeClick(ByVal Node As Object)

 
    
    
    
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
For those interested, I was able to figure it out. The nodSelected.Key was found and then I trimed the first letter off to give me the matching PK ID. Works great

Code:
Dim nodSelected As MSComctlLib.Node ' a variable for the currently selected node
    
    Set nodSelected = Me.xTree.SelectedItem ' get the currently selected node
    
    Me.txtSelNode = nodSelected.Key
    Me.txtKeyFix = Right(nodSelected.Key, Len(nodSelected.Key) - 1)

Seems I need to post more often, As soon as I post I usually get somewhere!
 
Upvote 0
How often does laying out a question logically present you with the answer? For me, very...

Denis
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top