Results 1 to 6 of 6

Thread: Key Not Unique [Resolved]

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2003
    Location
    Georgia
    Posts
    32

    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:
    1. Private Sub initializeTree()
    2.     tvwItems.ImageList = imgList
    3.    
    4.     Dim nodClass As Node
    5.     Dim nodTitle As Node
    6.    
    7.     tvwItems.LineStyle = tvwRootLines
    8.     tvwItems.Nodes.Clear
    9.    
    10.     'Initialize the recordsets
    11.     Set rsClass = New ADODB.Recordset
    12.     Set rsTitle = New ADODB.Recordset
    13.  
    14.     'Open the Class recordset
    15.     strSQLClass = "SELECT id, classification FROM tblClassification ORDER BY classification"
    16.     rsClass.Open strSQLClass, conTree, adOpenStatic, adLockOptimistic
    17.    
    18.     'set node for treeview
    19.     If rsClass.RecordCount > 0 Then
    20.         While Not rsClass.EOF
    21.             Set nodClass = tvwItems.Nodes.Add(, , (rsClass.Fields("classification")), (rsClass.Fields("classification")), 1)
    22.  
    23.             'Opens the recordset for Title
    24.             strSQLTitle = "SELECT id, title, classification, code, dateadded FROM tblSnipplets WHERE classification LIKE '" & (rsClass.Fields("classification")) & "%'"
    25.             rsTitle.Open strSQLTitle, conTree, adOpenStatic, adLockOptimistic
    26.            
    27.             'set node for treeview
    28.             If rsTitle.RecordCount > 0 Then
    29.                 While Not rsTitle.EOF
    30.                     Set nodTitle = tvwItems.Nodes.Add((rsClass.Fields("classification")), tvwChild, CStr(rsTitle.Fields("id")) & " id", (rsTitle.Fields("title")), 2)
    31.                   '  Set nodTitle = tvwItems.Nodes.Add
    32.                    
    33.                     nodTitle.Tag = rsTitle.Fields("Id").Value
    34.                     rsTitle.MoveNext
    35.                     DoEvents
    36.                 Wend
    37.             End If
    38.             rsTitle.Close
    39.            
    40.             rsClass.MoveNext
    41.             DoEvents
    42.         Wend
    43.     End If
    44.     rsClass.Close
    45. End Sub
    Last edited by odamsr; Apr 11th, 2004 at 11:15 AM.

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2003
    Location
    Georgia
    Posts
    32
    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:
    1. Private Sub initializeTree()
    2.     tvwItems.ImageList = imgList
    3.    
    4.     Dim nodClass As Node
    5.     Dim nodTitle As Node
    6.    
    7.     tvwItems.LineStyle = tvwRootLines
    8.     tvwItems.Nodes.Clear
    9.    
    10.     'Initialize the recordsets
    11.     Set rsClass = New ADODB.Recordset
    12.     Set rsTitle = New ADODB.Recordset
    13.  
    14.     'Open the Class recordset
    15.     strSQLClass = "SELECT id, classification FROM tblClassification ORDER BY classification"
    16.     rsClass.Open strSQLClass, conTree, adOpenStatic, adLockOptimistic
    17.    
    18.     'set node for treeview
    19.     If rsClass.RecordCount > 0 Then
    20.         While Not rsClass.EOF
    21.             Set nodClass = tvwItems.Nodes.Add(, , (rsClass.Fields("classification")), (rsClass.Fields("classification")), 1)
    22.  
    23.             'Opens the recordset for Title
    24.             strSQLTitle = "SELECT id, uniquekey, title, classification, code, dateadded FROM tblSnipplets WHERE classification LIKE '" & (rsClass.Fields("classification")) & "%'"
    25.             rsTitle.Open strSQLTitle, conTree, adOpenStatic, adLockOptimistic
    26.            
    27.             'set node for treeview
    28.             If rsTitle.RecordCount > 0 Then
    29.                 While Not rsTitle.EOF
    30.                     Set nodTitle = tvwItems.Nodes.Add((rsClass.Fields("classification")), tvwChild, (rsTitle.Fields("uniquekey")), (rsTitle.Fields("title")), 2)
    31.                   '  Set nodTitle = tvwItems.Nodes.Add
    32.                    
    33.                     nodTitle.Tag = rsTitle.Fields("Id").Value
    34.                     rsTitle.MoveNext
    35.                     DoEvents
    36.                 Wend
    37.             End If
    38.             rsTitle.Close
    39.            
    40.             rsClass.MoveNext
    41.             DoEvents
    42.         Wend
    43.     End If
    44.     rsClass.Close
    45. End Sub

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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)

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    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.

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2003
    Location
    Georgia
    Posts
    32
    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
  •  



Click Here to Expand Forum to Full Width