Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate Treeview from Access DB recursive

Status
Not open for further replies.

ZmrAbdulla

Technical User
Apr 22, 2003
4,364
AE
Can anyone give me an example to to populate a treeview from an access db? My searches reach nowhere.

consider me as beginner in coding.

using VB2005 express.

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
Thanks.. I have got all those 3 and more when searching. Still not matching the one that I require. I am looking for something to fill a treeview recursively irrespective of field count.

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
Can you give us some more idea...what are you planning?
I am looking for something to fill a treeview recursively irrespective of field count.

Sharing the best from my side...

--Prashant--
 
Here's something that may help with the recursive bit. This function searches the tree for a node by name (although you could probably adapt it to pick up something else say from the node.tag property). I use this to find the parent node that each new node should be added to - essential when building a tree from data.
Code:
    Private Function treenodeSearch(ByVal name As String, Optional ByVal node As TreeNode = Nothing) As TreeNode
        Dim snode As TreeNode, ssnode As TreeNode

        If node Is Nothing Then
            For Each snode In Nodes
                'checknode
                If snode.Text = name Then
                    Return snode
				Else
					'has nodes?
					If snode.GetNodeCount(False) > 0 Then
						ssnode = treenodeSearch(name, snode)
						If ssnode.Text <> snode.Text Then
							Return ssnode
						End If
					End If
				End If
			Next

        Else
            For Each snode In node.Nodes
                'checknode
                If snode.Text = name Then
                    Return snode
				Else
					'has nodes?
					If snode.GetNodeCount(False) > 0 Then
						ssnode = treenodeSearch(name, snode)
						If ssnode.Text <> snode.Text Then
							Return ssnode
						End If
					End If
				End If
			Next
        End If
        'failed
        Return node

    End Function
The optional byval node argument is to support recursive searches so this is how you would use it:
Code:
                parentnode = treenodeSearch("ParentName")
                parentnode.Nodes.Add(childNode)
 
using overloading that would result in this

Code:
Private Function treenodeSearch(ByVal name As String) As TreeNode
        Dim ssnode As TreeNode
            For Each snode as Treenode In Nodes
                'checknode
                If snode.Text = name Then
                    Return snode
                Else
                    'has nodes?
                    If snode.GetNodeCount(False) > 0 Then
                        ssnode = treenodeSearch(name, snode)
                        If ssnode.Text <> snode.Text Then
                            Return ssnode
                        End If
                    End If
                End If
            Next
End Function

Private Function treenodeSearch(ByVal name As String, Optional ByVal node As TreeNode = Nothing) As TreeNode
            For Each snode as Treenode In node.Nodes
                'checknode
                If snode.Text = name Then
                    Return snode
                Else
                    'has nodes?
                    If snode.GetNodeCount(False) > 0 Then
                        ssnode = treenodeSearch(name, snode)
                        If ssnode.Text <> snode.Text Then
                            Return ssnode
                        End If
                    End If
                End If
            Next
    End Function

But anyway, I would need to see the structure of the records. Are they in no particular order or can you order them so the children follow the parents in an orderly fashion. Show us some data. And we all know you are not a novice.

Christiaan Baes
Belgium

"My new site" - Me
 
I have a table that have four fields. Say field1 to field4
Field1 & 2 will have data always. 3 & 4 will not be filled with data always. So the data will be like below when it populated to treeview
Code:
Node1
	Node1-Step1
		Node1-Step2
		Node1-Step2
	Node1-Step1
		Node1-Step2
	Node1-Step1

Node2
	Node2-Step1
		Node2-Step2
			Node2-Step3		
	Node2-Step1
	
	Node2-Step1
		Node2-Step2
		Node2-Step2

Node3
	Node3-Step1
		Node3-Step2
		Node3-Step2
			Node3-Step3
	Node3-Step1
	
	Node3-Step1

Node4

I will try to adapt techsmith's sample after a while. Let me see how it works

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
I still don't get how the data are stored in the database. Could you give an exampl of that. What is in the fields? Strings?

Christiaan Baes
Belgium

"My new site" - Me
 
All are string. All in one table/Query Just like excel sheet.
Group Administration |Admin | Receptions
Group Administration |Admin
Group Administration |Admin | Security


It will look like...
Code:
-Group Administration
	-Admin
		-Receptions
		-Security
		....
	-Public Relations
	-Personnel Dept
	-......

-CompanyName1
	-Head Office
		-Sales Admin
			-Equipment Sales
			
		-Accounts
		-...
		-...
					
	-Branches
		-BranchName1	
			-Sales Office
			-Ware House

		-BranchName2	
			-Sales Office
			-Work Site
		-Workshop
-CompanyName2

......
......

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
So you want to feed it a datatable I presume
So I see no need to do it recursively.
You just need to do a for each and order/sort them correctly



Christiaan Baes
Belgium

"My new site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top