Add Child Node to Treeview

A

accessuser1308

I currently have a form with a treeview control for a family tree database I
have made. I have the tree generated from a table. The key for my tree is
WhoID. I can make a tree that links WhoID with a MotherID and a separate
tree that will link the same WhoID with FatherID. I would like to make a
tree that will have the "child" WhoID associated with both MotherID and
FatherID, such that when someone follows the family tree the child is
associated with both "branches" of the family tree. Is this possible to do?
How would it be done? Your help is greatly appreciated.

Thank you
 
S

S.Clark

Thinking while typing...

I would have tables structured as the following:

tblPerson
----------
PersonID
PersonLName
PersonFName
etc...

tblRelations
-----------
PersonID
ParentID (Reference to PersonID)
RelationTypeID

tlkpRelationType
-----------------
RelationTypeID
RelationTypeName

This allows for a person to have multiple parental units (e.g. I had 4), and
you can specify the relationship type via the RelationTypeID. (Son, Daughter,
Step-xxx, Foster, Adopted, Ferrel, etc) You can add the RelationTypeName to
the Node if you'd like.

Then query appropriately to generate the tree nodes.
 
A

accessuser1308

Thank you for your reply. I had actually tried this approach. My problem is
the query will now have multiple copies of WhoID, based on the number of
parents in the associated table. When I base the tree on this query I get an
error that the index is out of range. The code I am using is below:

'Fill Tree
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strKey As String

Set db = CurrentDb
Set rs = db.OpenRecordset( _
"SELECT * " & _
"FROM qryFamTree " & _
"ORDER BY qryFamTree.BDate, qryFamTree.WhoID", dbOpenSnapshot,
dbReadOnly Or dbForwardOnly)
TreeView0.Nodes.Clear

With rs
'Loop through all FamTree-items, adding them to the treeview
While Not .EOF
'Assemble a key for this node, consisting of three arguments
‘ndObjectType and ndObjectName will allow me to open a picture or document
when node is clicked – this works fine
strKey = !WhoID & ";" & Nz(!ndObjectType) & ";" & Nz(!ndObjectName)

If !Parent > 0 Then
'This node is a child-node of some other node;
'Find the parent node and add it below that node's last child
TreeView0.Nodes.Add(getNodeIndex(!Parent), tvwChild, strKey,
!Last & !Suff & ", " & !First & " m. " & !SpouseName).ForeColor = 16711680
', CStr(!ndImage)
Else
'There is no parent - add this node to the treeview's root
TreeView0.Nodes.Add , tvwLast, strKey, !Last & !Suff & ", " &
!First & " m. " & !SpouseName ', CStr(!ndImage)
End If

.MoveNext
Wend
End With
'Clean up
Set rs = Nothing
Set db = Nothing
End Sub
‘*************************************************************
Private Function getNodeIndex(ByVal strKey As String) As Integer
Dim intCounter As Integer

'Assume failure (return root)
getNodeIndex = 0

For intCounter = 1 To TreeView0.Nodes.Count
'The key being looked for is the first argument
If Split(TreeView0.Nodes(intCounter).Key, ";")(0) = strKey Then
'Found the node
getNodeIndex = intCounter
Exit For
End If
Next intCounter
End Function


qryFamTree has my main table joined to a second table by WhoID. The second
table consists of the parent IDs for each parent the person in the main table
may have. In the query (if the individual had 4 parents) there would be 4
entries for the individual, each one with a different parentID. The form
code errors out when it tries to place the child with a second parent in the
tree.

Thank you for any suggestions
 
D

Douglas J. Steele

You cannot use the same value for strKey for multiple nodes. You'll need to
generate different values, even though they refer to the same child.
 
G

GB

is the problem in the code that includes the update of the ForeColor? Does
this area of code get run at least once before the error actually appears?

Why is it in this area of the code that there are parentheses after the add,
but in the following line that adds the information (when no parent is found)
there are no parentheses? Does the forecolor need to be set here? (If it
could be the problem.) Why not set it after adding the individual at least
for testing purposes? Could create another variable to capture the return of
the add function and then use it to modify the forecolor if need be.
 
G

GB

With regards to the index going out of range, again, since I do not seem to
be able to find the information necessary to talk specifically about the
treeview class, most arrays are designed to begin at 0 and be addressed up to
the count of items - 1. Therefore, you may be getting an error in your
getNodeIndex because you loop from 1 to TreeView0.Nodes.Count instead of from
0 to TreeView0.Nodes.Count - 1. (Alternatively you could subtract 1 from
wherever you use intCounter)

I also can not speak for what Mr. Douglas J. Steele has said, but was
thinking yesterday afternoon that perhaps one way to get a unique strKey is
to also include something like the parent's ID, or a random number. In
addition, include some error trapping that basically if the assignment of the
random number and remaining information of the strKey causes an error that a
new random number be generated and the strKey be reformulated to be added
into the !Parent section. :/ Just an idea if the strKey is not unique as a
reason for it not working.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top