PDA

View Full Version : TreeView Form Text Reference



tammyl
11-17-2010, 01:46 AM
Hi,

I'm trying to create a treeview form. I've setup the form & now creating the Parent & Child Nodes.

I have three tables.
tAuthors - AuthorID, Firstname, lastname
tBooks - BookID, title, etc
tBookAuthors - AuthorID, BookID

tBookAuthors has the link relationship between tAuthors & tBooks so that i can have many authors for one book.

How do i display text from tAuthors & tBooks in my treeview.

I'm using tBookAuthors ad the main recordset because that's where the relationship is??

Following is my vba code i'm using but i'm getting an error mesage.
Run-time error '3265' Item not found in collection.

I know it's got to do with how i'm referencing the 'text' part of the code.

Private Sub CreateParentNodes()
Dim rst As DAO.Recordset ' recordset for category data

' open the recordset for categories
Set rst = CurrentDb.TableDefs!tBookAuthors.OpenRecordset

' loop through the rows in the recordset
rst.MoveFirst
Do Until rst.EOF
Me.TreeView1.Nodes.Add Text:=rst![tAuthors].[AuthorLastName], _
Key:="Cat=" & CStr(rst![tBookAuthors].[AuthorID])
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
End Sub

Private Sub CreateChildNodes()
Dim rst As DAO.Recordset ' recordset for product data

' open the recordset for products
Set rst = CurrentDb.TableDefs!tBookAuthors.OpenRecordset

' loop through the rows in the recordset
rst.MoveFirst
Do Until rst.EOF
Me.TreeView1.Nodes.Add Relationship:=tvwChild, _
Relative:="Cat=" & CStr(rst![tBookAuthors].[AuthorID]), _
Text:=rst![tBooks].[Title], Key:="Prod=" & CStr(rst![tBookAuthors].[BookID])
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
End Sub

Any help much appreciated. :bow:

Cheers
tammyl

orange
11-17-2010, 09:33 AM
Just looking at your sample code, and trying to recreate.

I just create a form (Form11) with a treeview control (treeview0). I have 3 tables AUTHOR,BOOK and BOOKAuthor.

Here is my code to populate the Parent Node - I haven't gotten to child etc yet. Haven't worked with treeview for long time, but will work on child nodes next.


'---------------------------------------------------------------------------------------
' Procedure : Form_Load
' Author : Jack
' Created : 11/17/2010
' Purpose :
'---------------------------------------------------------------------------------------
' Last Modified:
'
' Inputs: N/A
' Dependency: N/A
'------------------------------------------------------------------------------
'
Private Sub Form_Load()
'CreateParentNodes()
Dim rst As DAO.Recordset ' recordset for category data
Dim Nod As node '''''''''''''''''' need to identify a node
' open the recordset for categories
On Error GoTo Form_Load_Error

Set rst = CurrentDb.OpenRecordset("Author") 'syntax corrected
If Not rst.EOF Then rst.MoveFirst 'check for data added
' loop through the rows in the recordset and assign root nodes

Do Until rst.EOF
Set Nod = Me.TreeView0.Nodes.Add(, , _
Text:=CStr(rst![LastName]) _
,Key:="Cat=" & rst!AuthorID)
rst.MoveNext
Loop
rst.Close
Set rst = Nothing

On Error GoTo 0
Exit Sub

Form_Load_Error:

MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure Form_Load of VBA Document Form_Form11"
End Sub