Using Collection to populate TreeView...

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: RPK (RPK_at_discussions.microsoft.com)
Date: 01/26/05


Date: Tue, 25 Jan 2005 21:29:01 -0800

I want to display a Tree with City as Parent Node and Customers of that city
as Child to that node.
Example:

+CITY1
|...ABC
|...XYZ
+CITY2
|...DEF
|...PHQ

I wrote following code but it displays error at line "sCity =
CityColl.Item(CStr(i))".
Error: "Object is no longer valid".
I am using a collection which stores names of cities extracted by SQL and
then this
collection to populate TREE. The Collection is defined globally in the Form
and is filled with Cities as the
Form Loads.

Help me on how to arrange as above.

[code] Private Sub addtree()
    Dim TotCity, TotRec, i, j
    Dim SearchQuery As String
    Dim sCity
    Dim rsSelectCity As ADODB.Recordset
    
    'Count City Collection Items
    TotCity = CityColl.Count
    'Open recordset
    Set rsSelectCity = New ADODB.Recordset
    
    If rsSelectCity.State = adStateClosed Then
        rsSelectCity.Open "Select * from customer", conn, adOpenKeyset,
adLockReadOnly
    End If
    
    TotRec = rsSelectCity.RecordCount
    
    'Populate Tree
    For i = 1 To TotCity
        sCity = CityColl.Item(CStr(i))
        TreeView1.Nodes.Add , , "c" & i, UCase(sCity)
        'Update Query String Instantly
        SearchQuery = "Select CustID, CustName, Location From customer Where
City = '" & strCity & "'"
        For j = 1 To TotRec
            TreeView1.Nodes.Add "c" & i, , "c" & j, UCase(sCity)
        Next j
        
    Next

End Sub [/code]



Relevant Pages

  • DAG/POSET "consultancy fee" conjecture: sole thread here for all future postings
    ... Dim 2 posets and DAGs which are finite ... will map to the same dim 2 poset P (and that one extension of this ... poset will be 0,1,2,...n for a tree of n+1 vertices. ... finite rooted directed trees ...
    (sci.math)
  • Re: Treeview performance optimize
    ... Do not loading the tree with icons only speeds up the filling a little ... Dim myTree As TreeView ... Dim iRS, rsRecordCount As Integer ...
    (microsoft.public.vb.general.discussion)
  • Re: Changing the properties of a Shape/ AutoShape within a cell
    ... Dim nResult As Long ... drawing were named but when I asked excel to select a certain tree, ... Private Sub CommandButton3_Click ... Dim NewTree As Shape ...
    (microsoft.public.excel.programming)
  • Re: symbolic and literal things in Revelation
    ... In the book of Revelation we find ... So there will be a real city, a tree of life and a new earth. ...
    (uk.religion.christian)
  • Re: NotInList
    ... Response As Integer) ... Dim intAnswer As Integer ... Please be sure to spell the city correctly if you choose to add it." ... MsgBox "The new city has been added to the list.", vbInformation, ...
    (microsoft.public.access.formscoding)