Consulting

Results 1 to 5 of 5

Thread: Solved: Populate Treeview

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location

    Solved: Populate Treeview

    All

    Im trying to populate a treeview on a userform from an access table using the code below

    I want the parent to be the table(Recordset) name and the child to be the contents of the column FieldName2 but Im getting nowhere

    My code works and I ve figured out how to populate it with the table column titles but what i want to be able to do is list the records in the columns

    Anyone got any ideas as I cant find the answer to this anywhere

    Thanks

    Gibbo

    [VBA] Sub Populate_Treeview()
    Dim cnt As ADODB.Connection
    Dim rst As New ADODB.Recordset
    Dim strDb As String, strSQL As String
    Dim xlCalc As XlCalculation
    Dim j As Long, x As Long

    'SET YOUR VARIABLES HERE:
    'Path to the database and SQL-statement to execute
    strDb = "C:\Gibbos.mdb"
    strSQL = "SELECT tbl_TEST.* From tbl_Test"

    'In order to increase performance
    With Application
    xlCalc = .Calculation
    .EnableEvents = False
    .ScreenUpdating = False
    End With

    'Establish and ADO connection
    Set cnt = New ADODB.Connection

    'Create the connection string
    cnt.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & strDb & ";"

    'Retrieve the recordset and count fields & records
    rst.Open strSQL, cnt
    'Add the items to the listbox
    With UserForm1
    UserForm1.TreeView1.Nodes.Add , , "Table", "Table"
    'Child
    For x = 0 To rst.Fields.Count - 1
    UserForm1.TreeView1.Nodes.Add "Table", 4, , rst.Fields(x).Name
    Next x
    End With
    AddCounter

    'Close the recordset, connection and release objects from memory
    With rst
    .Close
    .ActiveConnection = Nothing
    End With
    cnt.Close
    Set rst = Nothing
    Set cnt = Nothing

    'Restore the settings
    With Application
    .Calculation = xlCalc
    .EnableEvents = True
    .ScreenUpdating = True
    End With
    End Sub
    [/VBA]
    Last edited by gibbo1715; 10-22-2005 at 11:12 AM. Reason: change to code

Posting Permissions

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