Note that there are some explanatory texts on larger screens.

plurals
  1. POExcel VBA - Create Treeview from recordset
    text
    copied!<p>I have a SQL 05 Stored Procedure that brings back around 180 rows with the structure:</p> <p>ID | Name | ParentId.</p> <p>What I would like to do is create a Treeview based on the records returned. Below is a sample I adapted from elsewhere on the forum (<a href="https://stackoverflow.com/questions/1074004/build-a-tree-like-representation-of-data-in-excel">here</a>)</p> <p>I nearly have it doing what I want but not quite.</p> <p>This is the problem I have, The root is added ok. The next layer down is added ok. However, it does not know what the next ParentId should be. Should I just incremement a variable and try and find matches? I guess I should include that in the top method.</p> <p>Hopefully I am not far off and need a shove in the right direction,</p> <p>Many Thanks in advance Michael</p> <p>As a final thing, the BOF AND EOF checks are there in the loop as I seem to have had a lot of errors being thrown if I don't have it. </p> <pre><code>Private Sub MakeTree(ByVal rs As ADODB.Recordset) rs.MoveFirst Do Until rs.EOF If (IsNull(rs.Fields("ParentID"))) Then Call TVFunds.Nodes.Add(, , "Key" + CStr(rs.Fields("Id")), rs.Fields("Name")) Call MsgBox("Key" + CStr(rs.Fields("Id")) + " " + rs.Fields("Name"), vbInformation, "Added Root") Else DrawNode rs, rs.Fields("ParentID"), rs.Fields("ID") End If If rs.BOF &lt;&gt; True And rs.EOF &lt;&gt; True Then rs.MoveNext End If Loop End Sub Private Sub DrawNode(ByRef r As ADODB.Recordset, ByRef pId As Integer, ByRef Id As Integer) r.MoveFirst Do Until r.EOF If (r.Fields("ParentId") = pId And r.Fields("Id") = Id) Then Call TVFunds.Nodes.Add("Key" + CStr(r.Fields("ParentId")), tvwChild, "Key" + CStr(r.Fields("Id")), r.Fields("Name")) Call MsgBox("Key" + CStr(r.Fields("ParentId")) + " Key" + CStr(r.Fields("Id")) + " " + r.Fields("Name"), vbInformation, "Added") Id = Id + 1 DrawNode r, pId, Id End If If r.BOF &lt;&gt; True And r.EOF &lt;&gt; True Then r.MoveNext End If Loop End Sub </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload