|
-
Apr 9th, 2004, 08:39 AM
#1
Thread Starter
Member
Key Not Unique [Resolved]
I get a Key not Unique error message when I add a record to a parent node without a child node, but when I add a record to a parent node that already has a child it works.
VB Code:
Private Sub initializeTree()
tvwItems.ImageList = imgList
Dim nodClass As Node
Dim nodTitle As Node
tvwItems.LineStyle = tvwRootLines
tvwItems.Nodes.Clear
'Initialize the recordsets
Set rsClass = New ADODB.Recordset
Set rsTitle = New ADODB.Recordset
'Open the Class recordset
strSQLClass = "SELECT id, classification FROM tblClassification ORDER BY classification"
rsClass.Open strSQLClass, conTree, adOpenStatic, adLockOptimistic
'set node for treeview
If rsClass.RecordCount > 0 Then
While Not rsClass.EOF
Set nodClass = tvwItems.Nodes.Add(, , (rsClass.Fields("classification")), (rsClass.Fields("classification")), 1)
'Opens the recordset for Title
strSQLTitle = "SELECT id, title, classification, code, dateadded FROM tblSnipplets WHERE classification LIKE '" & (rsClass.Fields("classification")) & "%'"
rsTitle.Open strSQLTitle, conTree, adOpenStatic, adLockOptimistic
'set node for treeview
If rsTitle.RecordCount > 0 Then
While Not rsTitle.EOF
Set nodTitle = tvwItems.Nodes.Add((rsClass.Fields("classification")), tvwChild, CStr(rsTitle.Fields("id")) & " id", (rsTitle.Fields("title")), 2)
' Set nodTitle = tvwItems.Nodes.Add
nodTitle.Tag = rsTitle.Fields("Id").Value
rsTitle.MoveNext
DoEvents
Wend
End If
rsTitle.Close
rsClass.MoveNext
DoEvents
Wend
End If
rsClass.Close
End Sub
Last edited by odamsr; Apr 11th, 2004 at 11:15 AM.
-
Apr 10th, 2004, 09:49 PM
#2
The problem has nothing to do with there being a child node or not. The key value must be unique throughout the whole tree. Click the Generate unique TreeView keys link in my signature for a way to ensure that all keys are unique.
-
Apr 11th, 2004, 10:29 AM
#3
Thread Starter
Member
I implemented the unique key function from your post and I am still getting the key not unique when I add a child.
VB Code:
Private Sub initializeTree()
tvwItems.ImageList = imgList
Dim nodClass As Node
Dim nodTitle As Node
tvwItems.LineStyle = tvwRootLines
tvwItems.Nodes.Clear
'Initialize the recordsets
Set rsClass = New ADODB.Recordset
Set rsTitle = New ADODB.Recordset
'Open the Class recordset
strSQLClass = "SELECT id, classification FROM tblClassification ORDER BY classification"
rsClass.Open strSQLClass, conTree, adOpenStatic, adLockOptimistic
'set node for treeview
If rsClass.RecordCount > 0 Then
While Not rsClass.EOF
Set nodClass = tvwItems.Nodes.Add(, , (rsClass.Fields("classification")), (rsClass.Fields("classification")), 1)
'Opens the recordset for Title
strSQLTitle = "SELECT id, uniquekey, title, classification, code, dateadded FROM tblSnipplets WHERE classification LIKE '" & (rsClass.Fields("classification")) & "%'"
rsTitle.Open strSQLTitle, conTree, adOpenStatic, adLockOptimistic
'set node for treeview
If rsTitle.RecordCount > 0 Then
While Not rsTitle.EOF
Set nodTitle = tvwItems.Nodes.Add((rsClass.Fields("classification")), tvwChild, (rsTitle.Fields("uniquekey")), (rsTitle.Fields("title")), 2)
' Set nodTitle = tvwItems.Nodes.Add
nodTitle.Tag = rsTitle.Fields("Id").Value
rsTitle.MoveNext
DoEvents
Wend
End If
rsTitle.Close
rsClass.MoveNext
DoEvents
Wend
End If
rsClass.Close
End Sub
-
Apr 11th, 2004, 10:46 AM
#4
Are you trying to add a unique key with this???
...(rsTitle.Fields("uniquekey"))...
How are you generating the uniquekey field?
If you've looked at my code, what you really want to do is
Set nodTitle = tvwItems.Nodes.Add((rsClass.Fields("classification")), tvwChild, UniqueKey, (rsTitle.Fields("title")), 2)
-
Apr 11th, 2004, 11:04 AM
#5
The problem could be with your Select statement. Since you are doing a Like on the Classification field, the rsTitle recordset could contain records that were already used by another parent node.
The reverse could also be true. Your code will try to add child nodes to parent nodes that don't exist yet.
-
Apr 11th, 2004, 11:14 AM
#6
Thread Starter
Member
I fixed it. I was using the recordset of unique key instead of the function. Thanks for your help.
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
|