Full Version: Treeview problem
UtterAccess Forums > Microsoft® Access > Access Forms
Tomo1
I have a treeview sample database that I in fact downloaded from UA, and by copying the methodology, trying to put my own tree view in place for a contacts database.
I have got the form with a tree view object on it and I paste in the same code....modify the table and field names etc and then when I go to run it tells me that user defined object type is not defined for node.
It works fine in the demo and I can't find anything where node is defined here. Where should I be looking or am I missing something?
Opresume I don't need Access developer edition?
adaytay
Hi Tomo,
k... no, you don't need Access Developer system.
If you like, zip up your db (to under 500K), and attach to a post. I'll take a look at the code for you.
Ad
Clippit
You may just be missing a reference. I think for the treeview it's "Microsoft Windows Common Controls". Check in Tools/References.
Tomo1
Thanks! It was a missing reference - I'd just counted the number of references in each rather than checking they were the same for each database. I then had to browse for the Common Controls as it wasn't in the list. Problem sorted.
My second problem is how to get a second tier after the first tier. My code is as follows:-
Private Sub Form_Load()
Dim conn As Connection
Dim rstChild As Recordset
Dim rst As Recordset
Dim objNode As Node
Dim parNode As Node
Dim strKey As String
Dim strParent As String
Set conn = CurrentProject.Connection
Set rstChild = New Recordset
Set rst = New Recordset
'Add the department nodes
With rst
.Open "tbCustomer", conn, _
adOpenForwardOnly, adLockReadOnly
Do Until .EOF
strKey = .Fields("UniqueCustNo")
trvwTest.Nodes.Add Key:=strKey, _
Text:=.Fields("CustName")
.MoveNext
Loop
.Close
End With
Set rst = Nothing
'Loop through tblEmployees and add each
'employee to a node.
With rstChild
.Open "tbCustomerSites", conn, adOpenForwardOnly, _
adLockReadOnly
Do Until .EOF
strKey = .Fields("UniqueCustNo") & .Fields("SiteName")
strParent = .Fields("UniqueCustNo")
trvwTest.Nodes.Add relative:=strParent, relationship:=tvwChild, _
Key:=strKey, Text:=.Fields("SiteName") & ", " _
& .Fields("SiteAddress") & "," & .Fields("SiteTelephone")
.MoveNext
Loop
.Close
End With
conn.Close
Set rstChild = Nothing
Set conn = Nothing
End Sub
Also, is it possible to open forms or perform actions based on, say, double clicking an item in the tree?
Clippit
epends on whether your data is hierarchical (customer sites within customer sites) or some other kind of data. What do you want to use for your next level down?
Yes- I'll dig up a good sample for you later on today.
Clippit
Here are some examples:
This demo treeview display employees in a hierarchy. Each node's key is a single letter and then the employee number. In each case the employee form is opened filtered on the employee id which is ulled out of the key uding mid().
1. Open the employee form when the user clicks a node:
==================================
Private Sub xTree_NodeClick(ByVal Node As Object)
DoCmd.OpenForm "frmEmployees", , , BuildCriteria("EmployeeID", dbLong, Mid(Node.Key, 2))
End Sub
==================================
2. Open the employee form when the user clicks a button, based on the currently selected node:
==================================
Private Sub cmdShowDetails_Click()
With Me.xTree.SelectedItem
DoCmd.OpenForm "frmEmployees", , , BuildCriteria("EmployeeID", dbLong, Mid(.Key, 2))
End With
End Sub
==================================
3. Open the employee form when the user double clicks a node (this one's a little messy because double click normally expands/collapses nodes- I usually don't do double click with treeviews, but I think this code will work if you want to do it that way. it "undoes" the expand/collapse that will have happened automatically.
==================================
Private Sub xTree_DblClick()
With Me.xTree.SelectedItem
.Expanded = Not .Expanded
DoCmd.OpenForm "frmEmployees", , , BuildCriteria("EmployeeID", dbLong, Mid(.Key, 2))
End With
End Sub
==================================
Tomo1
Thanks, the code for clicking nodes looks good - it didn't occur to me about the double click - perhaps I'll leave that one. I'll try them.
o far as the data goes I have arranged it as follows: Customers which contain customer sites which contain people. One to many to many. So would it be hierachical but not recursive?
Tomo1
Also....if I have three levels of different types Customers, Sites, People then is it also possible to pick up which form to open based on which level of node is selected?
Tomo1
Now I've tried the the code for clicking a node to open a form. I don't get any error messages but nothing happens.
can't find a Nodeclick property so I just created a new sub (is this correct?). Also, does part of my key have to be a unique integer value - I notice that the build criteria asks for "FieldType as integer"??
Clippit
- it will not be recursive. I'd suggest that you need another loop to add the people to the sites- you should be able to extend the code you have above. I'd suggest adding a single letter at the beginning of each node key, and use a different letter for each level, that way you'll be able to tell from a node's key what type of node iI get a NodeClick event- not in the events tab of the property window but in the code window, by selecting the Treeview object in the first pull down and then the nodeclick event in the second.No- this is a constant that tells the data type- see the help for BuildCriteria- most common values there are DBText, DBLong, DBDate, DBInteger.
Tomo1
Thanks Steve - I think you've answered my questions...
Clippit
Excellent- you're welcome.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.