www.etjt.com 1
Form Management with the TreeView Control in Microsoft Access 2000
Presented to the
Access SIG
by Trevor Tregellas
June 4, 2002
www.etjt.com 2
Objectives
Show you a better way to manage multiple forms and complex data in a business application – an alternative to cascaded forms.
Demonstrate Object Oriented Programming
www.etjt.com 3
Agenda
Introduction to the control and syntax Simple Data, Drag and Drop Populate the TreeView from DB Use checkbox to open forms Manage multiple instances of various forms
with a User-defined forms collection Two ways to close a form Summary
www.etjt.com 4
Introduction
Hierarchical data – Customers
• Orders– …
Alternative to form/subform TreeView Popularized by “Windows
Explorer” and Visual Studio But docs and examples are hard to find
www.etjt.com 5
Add control to the form
www.etjt.com 6
References
Older versions do not work as well
www.etjt.com 7
Enable Checkboxes
www.etjt.com 8
What are Nodes?
Tree composed of nodes Node has a key (string, not numeric) Similar to a collection, but
Node also has relationships
www.etjt.com 9
Syntax
object.Add(relative, relationship, key, text, [image],[selectedimage])
www.etjt.com 10
Create an object variable to refer to the control on your form
Dim xTree As TreeView
Set xTree = Me.axTreeView.Object
' add a node
xTree.Nodes.Add …
www.etjt.com 11
Demo: Drag and drop
Based on KB article Manipulates table data directly
www.etjt.com 12
Populate Tree Level 1
' Fill Level 1 using CustomerID as the Key property. Only a key is needed
Set rs = db.OpenRecordset("Customers") Do Until rs.EOF
xTree.Nodes.Add , , rs!CustomerID, rs!CompanyName
rs.MoveNext
Loop
www.etjt.com 13
Populate Tree Level 2. Set rs = db.OpenRecordset("Orders") Do Until rs.EOF ' Link to Level 1 by referencing the CustomerID key and set ' the node as a child node of Level 1. Use "t" and the ' StrConv() function in the new Key property for Level 2, ' because OrderID is a numeric field.
strOrderKey = StrConv("t" & rs!OrderID, vbLowerCase) xTree.Nodes.Add CStr(rs!CustomerID), tvwChild,
strOrderKey, rs!OrderID & " " & rs!OrderDate
rs.MoveNext Loop
www.etjt.com 14
Possible errors when adding nodes
Duplicate key Relative not found
www.etjt.com 15
Use Checkbox to Open forms
Private Sub axTreeView_NodeCheck(ByVal node As Object)
'this fires for both check and uncheck If node.Checked Then Call OpenAForm(node) Else Call RemoveWhenUnchecked(node) End If End Sub
www.etjt.com 16
GetNodeLevel
Public Function GetNodeLevel(ByRef Node As Object) As Integer
'find out how deep the node is by walking up the tree until get to root node
If Node.Parent Is Nothing Then GetNodeLevel = 1: Exit Function
If Node.Parent.Parent Is Nothing Then GetNodeLevel = 2: Exit Function
If Node.Parent.Parent.Parent Is Nothing Then GetNodeLevel = 3: Exit Function
End Function
www.etjt.com 17
Open Which type of form?
Private Sub OpenAForm(ByVal node As Object) Select Case GetNodeLevel(Node)
Case 1
Call Me.NewCustomerForm(Node)
Case 2
Call Me.NewOrderForm(Node)
End Select
www.etjt.com 18
User-defined collection of forms
Needed because as soon as the reference to a new form instance goes out of scope, Access destroys the form (prove it!)
Private colForms As New Collection See Access 2000 Developers Handbook
Vol 1 pp547-553 Displaying Multiple Instances of Forms
www.etjt.com 19
Open a new Customer form…
Public Sub NewCustomerForm(CurNode As node)
'Form_ syntax only works IF form has a module!
Dim frm As Form_Customers
Set frm = New Form_Customers
www.etjt.com 20
form…
' The Key value must be a string
colForms.Add Item:=frm,
Key:= CStr (CurNode.Key)
'later use this key to remove from collection
' Pass along the filter information.
frm.Filter = "[CustomerID] = '" & CurNode.Key & "'"
www.etjt.com 21
form…
store key for later in form Tag property use to link back to node
frm.Tag = CurNode.Key
www.etjt.com 22
Two ways to close a form
1)Close Form from the TreeView by unchecking
2)Close the form directly by user action
Be orthogonal
www.etjt.com 23
1)Close Form from the TreeView
Private Sub RemoveWhenUnchecked( CurNode As node)
colForms.Remove CStr(CurNode.Key)
End Sub
www.etjt.com 24
2)Close the form directly
Public Sub RemoveInstance(frm As Form)' Each form calls this code when it closes itself, ' instead of being closed by unchecking a node. 'uncheck the node 'key was stored in tag when frm was created Me.axTreeView.Object.Nodes(frm.Tag).Checked
= vbFalse 'and remove form reference from collectioncolForms.Remove CStr(frm.Tag)
www.etjt.com 25
In the form to be closed
Private Sub Form_Unload(Cancel As Integer)
Call Form__frmListCustomers. RemoveInstance(Me)
End Sub
www.etjt.com 26
Summary
Key is a ? Non-numeric unique value
Keys are stored in TreeView Key also stored in form tag property Form references are in User-defined
collection Customer and Order Forms don’t know
about the TreeView or User-defined collection, they only call a public method of the TreeView form.
www.etjt.com 27
More Information
www.etjt.com Download Northwind tjt.mdb