Results 1 to 2 of 2
  1. #1
    lithium is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Dec 2013
    Posts
    72

    Key isnt unique

    I've been working on getting this treeview to work and have issues adding the third child, and eventually need a fourth as well. The first parent and the next child load fine but won't load that childs, child (stack will be four deep on a parent). I'm working with four tables each linked with one-to-many.



    Code:
    Option Compare DatabaseOption Explicit
    Dim rsCorp As DAO.Recordset
    Dim rsInfoSec As DAO.Recordset
    Dim rsFeature As DAO.Recordset
    Dim rsControl As DAO.Recordset
    Dim strSQL As String
    Dim strFSQL As String
    '----------------------
    Private Sub Form_Load()
    '----------------------
       ' set treeview control properties
       With Me.treeView
       
          ' set style property to value that allows pictures
          .Style = tvwTreelinesPlusMinusPictureText
          ' set to automatically expand selected node and collapse previous node
          '.SingleSel = True  ' Single Selection
       End With
       
       ' initialize tree
       TreeInit
    End Sub
    Private Sub TreeInit()
    
    
    Dim trvTree As Control
    Dim imgList As Control
    Dim nodObject As Node
    Dim i As Integer
    Dim rcount As Integer
    Dim strInfoSec As String
    Dim strCorp As String
    Dim strFeature As String
    Dim strControl As String
    Dim db As DAO.Database
    
    
       ' set database and recordset objects
       Set db = CurrentDb
      
        Set trvTree = Me.treeView
        Set imgList = Me.MyImageList
        
       ' set Treeview control ImageList property to Image List Object
       ' treeview object style was previously set to 7-TvwTreelinesPlusMinusPictureText
       ' and the images were loaded manually into the imagelist control. Rather
       ' than use the image keys "frame" and "form", the images'
       ' index numbers are used when adding nodes.
        
         trvTree.ImageList = imgList.Object
     
       ' use two recordsets, one for the parent and the other for the
       ' child.  First create parent node with the linking field as
       ' a root node key.  Next find the first and subsequent children
       ' matching the parent's linking field.  Use the parent node's
       ' node key as the child node's relative value. When you run out
       ' of children, move to next parent record and then find its children,
       ' and so forth until there are no more parents.  I supose this
       ' concept could be used with a third recordset to find children of
       ' the second recordset, and so on.
       
       With trvTree.Nodes
          
          ' clear any nodes, if any
          .Clear
          
          ' open needed recordsets
          Set rsCorp = db.OpenRecordset("tblCorp", dbOpenSnapshot)
          rsCorp.MoveLast
          rcount = rsCorp.RecordCount
          rsCorp.MoveFirst
          Set rsInfoSec = db.OpenRecordset("tblInfoSec", dbOpenSnapshot)
          Set rsFeature = db.OpenRecordset("tblFeatures", dbOpenSnapshot)
          
    ' loop through recordset and add nodes
    While Not (rsCorp.EOF)
    
    
       strCorp = rsCorp!Name
       strInfoSec = rsInfoSec!IS_Name
       strFeature = rsFeature!Description
       
       ' .. and add the parent node to Treeview
       Set nodObject = .Add(, , "A" & CStr(rsCorp!Policy_ID), strCorp, 1)
       strSQL = "Policy_ID = " & rsCorp!Policy_ID
       strFSQL = "InfoSec_FK = " & rsInfoSec!InfoSec_PK
    
    
          
       'find first child, if any, belonging to this parent
       rsInfoSec.FindFirst strSQL
       'on Error GoTo NoChild
       
       ' concatenate fields to create the child node text value.
       While Not (rsInfoSec.NoMatch)
            strInfoSec = rsInfoSec!IS_Name
          ' .. and add thee child node to the Treeview
             Set nodObject = .Add("A" & rsCorp!Policy_ID, tvwChild, _
                      "C" & rsInfoSec!IS_Name, strInfoSec, 2)
                      
             ' .. and create the node tag property holding key to child's baptism
             trvTree.Nodes("C" & rsInfoSec!IS_Name).Tag = rsInfoSec!InfoSec_PK
             
          ' Now find next child, if any
          rsFeature.FindFirst strFSQL
          While Not (rsFeature.NoMatch)
            strFeature = rsFeature!Description
            Set nodObject = .Add("C" & rsInfoSec!IS_Name, tvwChild, _
                "U" & rsFeature!Feature_PK, strFeature, 2)
            trvTree.Nodes("U" & rsFeature!Feature_PK).Tag = rsFeature!Feature_PK
            rsFeature.FindNext strFSQL
          Wend
          rsInfoSec.FindNext strSQL
    NoFeature:
        rsInfoSec.MoveNext
       ' .. and loop back and add next child
       Wend
    NoChild:
    
    
       ' no move children, so move to next parent
       rsCorp.MoveNext
       Wend
    End With
    Set rsCorp = Nothing
    Set rsInfoSec = Nothing
    Set rsFeature = Nothing
    End Sub
    Specifically, I get a Key is not unique error on this:
    Code:
            Set nodObject = .Add("C" & rsInfoSec!IS_Name, tvwChild, _
                "U" & rsFeature!Feature_PK, strFeature, 2)
    Any ideas where I went wrong or a better way to go about this? The key, "U" & rsFeature!Feature_PK is actually unique and thus confusing the hell out of me?!

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Don't know the answer, but best info on treeview is by SmileyCoder on Youtube. He has 6 videos with various topics

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 10
    Last Post: 11-08-2012, 08:03 PM
  2. Can anyone see why this isnt working please?
    By shabbaranks in forum Programming
    Replies: 3
    Last Post: 12-23-2011, 03:19 AM
  3. Could someone explain why this code isnt working please?
    By shabbaranks in forum Programming
    Replies: 7
    Last Post: 10-29-2011, 09:14 AM
  4. Replies: 7
    Last Post: 12-14-2010, 11:10 AM
  5. Combo Box isnt saving
    By calisen in forum Access
    Replies: 0
    Last Post: 09-11-2008, 03:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums