290 likes | 458 Views
Form Management with the TreeView Control in Microsoft Access 2000. Presented to the Access SIG by Trevor Tregellas June 4, 2002. Objectives. Show you a better way to manage multiple forms and complex data in a business application – an alternative to cascaded forms.
E N D
Form Management with the TreeView Control in Microsoft Access 2000 Presented to the Access SIG by Trevor Tregellas June 4, 2002 www.etjt.com
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
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
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
Add control to the form www.etjt.com
References • Older versions do not work as well www.etjt.com
Enable Checkboxes www.etjt.com
What are Nodes? • Tree composed of nodes • Node has a key (string, notnumeric) • Similar to a collection, butNode also has relationships www.etjt.com
Syntax object.Add(relative, relationship, key, text, [image],[selectedimage]) www.etjt.com
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
Demo: Drag and drop • Based on KB article • Manipulates table data directly www.etjt.com
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
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
Possible errors when adding nodes • Duplicate key • Relative not found www.etjt.com
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
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
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
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
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
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
form… • store key for later in form Tag property • use to link back to node frm.Tag = CurNode.Key www.etjt.com
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
1)Close Form from the TreeView Private Sub RemoveWhenUnchecked( CurNode As node) colForms.Remove CStr(CurNode.Key) End Sub www.etjt.com
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 collection colForms.Remove CStr(frm.Tag) www.etjt.com
In the form to be closed Private Sub Form_Unload(Cancel As Integer) Call Form__frmListCustomers. RemoveInstance(Me) End Sub www.etjt.com
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
More Information • www.etjt.com • Download Northwind tjt.mdb www.etjt.com