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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating a treeview from table

Status
Not open for further replies.

HandyAndy

Technical User
Jun 26, 2001
13
GB
Hi all

i have another question regarding treeviews
i want to populate the treeview with data from my table where the node name is the Fuction_Name. heres how i would do it in plain english:

For each record
treectl.nodes.add(Parent_ID,Relationship,Function_ID,Function_Name)

all this info is in the Function Table
ok so i am a newbie but someone must be able to help me? :)
Please?
Thanks
HandyAndy
 
Hi!

I know how to populate treeview from table, but I must know how did you organize the belongs of data.
What is Relationship and Function_ID?
Is there field what shown what's this bolonged to?

Maybe write several records data for understanding of table's structure and data organisation.

Aivars
 
Relationship is probably going to be tvwChild
Function_ID is a field from my database that i would like to use as the key part of the add method

can u help or do you have some information?

HandyAndy
 
Hi again!

I tried to change my function special for you. I'm not sure about some errors in this function, but do can check it and repair its. My table is more complicity because in function are several not needed variables. You can change its, too.

Function PopulateTreeView(trvTreeView As Object, strParent As String)
On Error GoTo Err_PopulateTreeView
Dim strSQL As String
Dim strFrom As String
Dim strWhere As String

Dim nodX As Node
Dim strParentKey As String
Dim strKey As String
Dim strNodeText As String

Dim i As Byte
Dim rst As Recordset
Dim errCount As Byte

TryAgain:
If strParent = "" Then
trvTreeView.Nodes.Clear
strSQL = "SELECT MyTable.Parent_ID, MyTable.Relationship, MyTable.Function_ID, MyTable.Function_Name "
strFrom = "FROM MyTable "
strWhere = "WHERE MyTable.Parent_ID is Null"
strSQL = strSQL & strFrom & strWhere & ";"
Set rst = CurrentDb.OpenRecordset(strSQL)
strKey = rst!Function_ID
strNodeText = rst!Function_Name
Set nodX = trvTreeView.Nodes.Add(, , strKey, strNodeText, strImageKey, "SELECTED")
nodX.ExpandedImage = "OPEN"
End If

'Parent_ID,Relationship,Function_ID,Function_Name
strSQL = "SELECT MyTable.Parent_ID, MyTable.Relationship, MyTable.Function_ID, MyTable.Function_Name "
strFrom = "FROM MyTable "
strWhere = "WHERE MyTable.Parent_ID='" & strParent & "' Order By MyTable.Function_ID"
strSQL = strSQL & strFrom & strWhere & ";"
Set rst = CurrentDb.OpenRecordset(strSQL)
If Not rst.EOF Then
strParentKey = strParent
While Not rst.EOF
strKey = rst!Function_ID
strNodeText = rst!Function_Name
Set nodX = trvTreeView.Nodes.Add(strParentKey, 4, strKey, strNodeText, strImageKey, "SELECTED")
nodX.ExpandedImage = "OPEN"
Call PopulateTreeView(trvTreeView, strKey)
rst.MoveNext
Wend
End If
rst.Close
Set rst = Nothing
trvTreeView.Nodes(1).Selected = True
trvTreeView.Nodes(1).Expanded = True

Exit_PopulateTreeView:
Exit Function
Err_PopulateTreeView:
If Err.Number = -2147417848 Then 'Some time at first time is generated error
If errCount < 3 Then
GoTo TryAgain
End If
End If
End Function

Good luck!
Aivars


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top