|
-
Oct 8th, 2004, 10:04 AM
#1
Thread Starter
Member
Element Not Found
It is bombing on the child node. I need a second set of eyes because I have looked at it and I can't see anything that is wrong.
Thanks.
Code:
'Declarations for treeview
Dim strKeyCat1 As String
Dim strTextCat1 As String
Dim ndCat1 As Node
Dim strKeyCat2 As String
Dim strTextCat2 As String
Dim ndCat2 As Node
'Initalize imagelist
TreeView1.ImageList = imgList
TreeView1.LineStyle = tvwRootLines
TreeView1.Indentation = 25
'Declarations for Data Connection & Recordsets
Dim rstCat1 As New ADODB.Recordset
Dim strSQLCat1 As String
Dim rstCat2 As New ADODB.Recordset
Dim strSQLCat2 As String
'Open data connection
dbconn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\code.mdb;User Id=admin;Password="
'set sql string for parent node
strSQLCat1 = "SELECT cat1 FROM tblCat1 ORDER BY Cat1"
'open rst for parent node
rstCat1.Open strSQLCat1, dbconn, adUseClient, adLockReadOnly
'clears the tree nodes
Me.TreeView1.Nodes.Clear
While Not rstCat1.EOF
strKeyCat1 = UniqueKey
strTextCat1 = rstCat1("cat1")
Set ndCat1 = TreeView1.Nodes.Add(, , strKeyCat1, strTextCat1, 1)
ndCat1.Tag = strKeyCat1
'set sql string for child node
strSQLCat2 = "SELECT Cat2, cat1 FROM tblCat2 " & _
"WHERE cat1 = '" & rstCat1("cat1") & "' " & _
"ORDER BY Cat2"
'open rst for child node
rstCat2.Open strSQLCat2, dbconn, adUseClient, adLockReadOnly
' If rstCat2.RecordCount > 0 Then
While Not rstCat2.EOF
strKeyCat2 = UniqueKey
strTextCat2 = rstCat2("cat2")
Set ndCat2 = TreeView1.Nodes.Add((rstCat1.Fields("cat1")), tvwChild, strKeyCat2, strTextCat2) <--- Element Not Found
ndCat2.Tag = strKeyCat2
rstCat2.MoveNext
Wend
rstCat2.Close
rstCat1.MoveNext
' End If
Wend
rstCat1.Close
Set dbconn = Nothing
End Sub
-
Oct 8th, 2004, 10:10 AM
#2
What is the value of UniqueKey? You use it as the Key when adding the Parent node (which will fail eventually as well because UniqueKey looks to be a constant). Keys must be unique.
The code to add a Child node fails because you are looking for an existing Parent Node whose Key = the Cat1 field. But, again the Parent nodes were created with this UniqueKey variable.
-
Oct 8th, 2004, 10:16 AM
#3
Thread Starter
Member
Thanks. I fixed it. Uniquekey is a function I run.. I got the code from this forum from a user...
Code:
Public Function UniqueKey() As String
'***************************************************************************
'Purpose: Generate a unique key for a Treeview.
'Inputs : None
'Outputs: The key for the Treeview
'***************************************************************************
UniqueKey = "P" & 1 + Int(Rnd() * 10000000)
End Function
-
Oct 8th, 2004, 10:45 AM
#4
That isnt guaranteed to be unique although the probability is low.
This would be better
VB Code:
Public Function UniqueKey() As String
Static lngCounter As Long
lngCounter = lngCounter + 1
UniqueKey = "P" & lngCounter
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|