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

    Treeview not populating right

    Okay, I am implementing a treeview and have it added to the form but can't seem to get it to populate the parents properly. Right now, it populates just the first row of the table (tblCorp).
    Code:
    Public Sub loadTreeview()
    
        Dim tv As MSComctlLib.TreeView
        Set tv = Forms("Main1").treeReqs.Object
        
    
        tv.Nodes.Clear
        Dim rsReqs As DAO.Recordset
        Set rsReqs = CurrentDb.OpenRecordset("SELECT * FROM tblCorp", dbOpenDynaset)
        
        Dim strFind As String
        strFind = "ID=1"
        rsReqs.FindFirst strFind
        Dim nodx As MSComctlLib.Node
        
        
        Do While Not rsReqs.NoMatch
        Set nodx = tv.Nodes.Add(, , , rsReqs!Policy_Name)
        
        rsReqs.FindNext strFind
        Loop
        
    End Sub
    The "ID" field is my primary key and is set to autoincrement in the table as new records are added. Whenever I attempt to just change strFind = "ID=1" to "ID=2" it gives me a type mismatch error....
    Last edited by June7; 11-15-2014 at 07:31 PM.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    That code doesn't increment the ID value to search. How does "ID=1" become "ID=2"?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    lithium is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Dec 2013
    Posts
    72
    Doesnt the .FindNext increment to the next one? Maybe that was my issue

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    strFind variable is still "ID=1"

    I've never used FindNext. All it does is find the next record that has that same criteria. If the ID value is unique then there isn't another record to find.

    I've never use treeview so not sure how this should be built. Maybe:

    Dim nodx As MSComctlLib.Node
    rsReqs.FindFirst "ID=1"
    If not rsRegs.NoMatch
    Do While Not rsReqs.EOF
    Set nodx = tv.Nodes.Add(, , , rsReqs!Policy_Name)
    rsReqs.MoveNext
    Loop
    End If

    Should the recordset have ORDER BY clause?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    lithium is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Dec 2013
    Posts
    72
    Yes, but strangely it started throwing errors left and right when I added the ORDER BY

  6. #6
    lithium is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Dec 2013
    Posts
    72
    Well, I got the parent to populate correctly by changing some things within my database tables (added Tree_ID and gave each of the four tables a primary identifier to illustrate what node they would be under) but now it gets stuck in children nodes by just looping the same parent node as if they were the children nodes (loops until no stack is available anymore)...man this is confusing since it is my first time with tree view.
    Code:
    Option Compare Database
    Public Sub loadTreeview()
    
        Dim tv As MSComctlLib.TreeView
        Set tv = Forms("Main1").treeReqs.Object
        
        tv.Nodes.Clear
        Dim rsReqs As DAO.Recordset
        Set rsReqs = CurrentDb.OpenRecordset("SELECT * FROM tblCorp", dbOpenDynaset)
        
        Dim strFind As String
        strFind = "Tree_ID=1"
        rsReqs.FindFirst strFind
        Dim nodX As MSComctlLib.Node
        
        Dim strBook As String
        Do While Not rsReqs.NoMatch
        Set nodX = tv.Nodes.Add(, , , rsReqs!Name)
        strBook = rsReqs.Bookmark
        addChildren tv, nodX, rsReqs, rsReqs!Policy_ID
        rsReqs.Bookmark = strBook
        rsReqs.FindNext strFind
        Loop
    
        
    End Sub
    Private Sub addChildren(tv As TreeView, nodParent As Node, rsReqs As DAO.Recordset, lngParentID As Long)
        Dim strFind As String
        strFind = "Tree_ID=" & lngParentID
        rsReqs.FindFirst strFind
        Dim nodX As Node
        Dim strBook As String
        Do While Not rsReqs.NoMatch
            Set nodX = tv.Nodes.Add(nodParent, tvwChild, , rsReqs!Name)
            strBook = rsReqs.Bookmark
            addChildren tv, nodX, rsReqs, rsReqs!Policy_ID
            rsReqs.Bookmark = strBook
            rsReqs.FindNext strFind
        Loop
    End Sub
    Table structures is this:
    Click image for larger version. 

Name:	Untitled.png 
Views:	15 
Size:	39.0 KB 
ID:	18754

    Essentially, each Corp Policy can have many InfoSec ones (they would be child nodes to the Corp Policy) and that goes down hill for the infoSec policies having many features (child node to that infosec)

    My guess is I linked back to the wrong PK and I honestly have no idea where to go from this point forward.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    I know zero about building/using treeview in Access. Most of what I find is Visual Basic related. However, this thread has example databases http://www.access-programmers.co.uk/...d.php?t=222000
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Treeview
    By Daniela in forum Forms
    Replies: 12
    Last Post: 01-24-2012, 04:07 AM
  2. Populating a Treeview with 6 levels
    By mrfixit1170 in forum Programming
    Replies: 1
    Last Post: 05-20-2011, 05:26 PM
  3. Treeview Example
    By pkstormy in forum Code Repository
    Replies: 0
    Last Post: 08-30-2010, 10:58 PM
  4. Treeview
    By eddiewills in forum Programming
    Replies: 0
    Last Post: 08-18-2010, 01:05 PM
  5. P&P Treeview
    By Teejay in forum Programming
    Replies: 0
    Last Post: 10-04-2009, 03:54 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