Article ID: 162523
Article Last Modified on 1/19/2007
162359 ACC97: Access 97 Data Outline ActiveX Control Available in Download Center
Name: axTreeView
Width: 3"
Height: 2"
Private Sub Form_Load()
Dim DB As Database, RS As RecordSet
Set DB = CurrentDb
' Open a Recordset and loop through it to fill the TreeView
' control.
Set RS = DB.OpenRecordset("Customers", dbOpenForwardOnly)
Do Until RS.EOF
Me!axTreeView.Nodes.Add , , RS!CustomerID, RS!CompanyName
RS.MoveNext
Loop
RS.Close
End Sub
Private Sub Form_Load()
Dim DB As Database, RS As RecordSet
Dim strOrderKey As String
Set DB = CurrentDb
' Fill Level 1 using CustomerID as the Key property.
Set RS = DB.OpenRecordset("Customers", dbOpenForwardOnly)
Do Until RS.EOF
Me!axTreeView.Nodes.Add , , RS!CustomerID, RS!CompanyName
RS.MoveNext
Loop
RS.Close
' Fill Level 2.
Set RS = DB.OpenRecordset("Orders", dbOpenForwardOnly)
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)
Me!axTreeView.Nodes.Add RS!CustomerID, tvwChild, strOrderKey, _
RS!OrderID & " " & RS!OrderDate
RS.MoveNext
Loop
RS.Close
' Fill Level 3.
Set RS = DB.OpenRecordset("Order Details", dbOpenForwardOnly)
Do Until RS.EOF
' Link to Level 2 by referencing the strOrderKey key and set
' the node as a child node of Level 2.
strOrderKey = StrConv("t" & RS!OrderID, vbLowerCase)
Me!axTreeView.Nodes.Add strOrderKey, tvwChild, RS!ProductID, _
RS!ProductID & " " & Format(RS!UnitPrice, "Currency")
RS.MoveNext
Loop
RS.Close
End Sub
Private Sub axTreeView_NodeClick(ByVal Node As Object)
DoCmd.OpenForm "Customers", , , "[CustomerID] = '" & Node.Key & "'"
End Sub
You can also add the same functionality to a button on your form, as the
following example illustrates.
Name: cmdOpenForm
Caption: View Details
OnClick: [Event Procedure]
Private Sub cmdOpenForm_Click()
Dim CurNode As Node
Set CurNode = Me!axTreeView.SelectedItem
On Error GoTo cmdOpenForm_Error
' Evaluate the Key value of the selected node.
Select Case Len(CurNode.Key)
' All CustomerID keys are 5 characters long.
Case 5
DoCmd.OpenForm "Customers", , , "[CustomerID] = '" & _
CurNode.Key & "'"
' All OrderID keys are 6 characters long.
Case 6
DoCmd.OpenForm "Orders", , , "[OrderID] = " & Mid _
(CurNode.Key, 2)
' Anything else must be an Order Detail record.
Case Is > 6
' Extract the ProductID from the node key to use in the
' Where condition when you open the Products form.
Dim i As Integer
i = InStr(CurNode.Key, "p")
DoCmd.OpenForm "Products", , , "[ProductID] = " & Mid _
(CurNode.Key, i + 1)
End Select
Exit Sub
cmdOpenForm_Error:
Select Case Err
' If error is because nothing is selected in TreeView.
Case 91
MsgBox "Please select an item in the TreeView control."
Case Else
MsgBox "Error: " & Err & vbCr & Err.Description
End Select
Exit Sub
End Sub
Forms!MyForm!MyTreeView.SelectedItem.Text = Forms!Customers!CompanyNameThis method is faster than clearing and refilling the entire TreeView control when only one record has changed.
Forms!MyForm!MyTreeView.SelectedItem.Key = Forms!Customers!CustomerID
Data Outline Control Event TreeView Control Event
---------------------------------------------------
AfterCollapse Collapse
AfterExpand Expand
AfterFormClose <none>
AfterFormOpen <none>
AfterFormUpdate <none>
AfterMove <none>
AfterRefresh <none>
AfterRequery <none>
AfterSelChange NodeClick
AfterStartup <none>
DoKeyPress KeyPress
DoRowClick <none> (Closest events are Click and
MouseDown)
DoRowDblClick <none> (Closest event is DblClick)
Enter Enter
ErrorEvent <none>
Exit Exit
FailCollapse <none>
FailExpand <none>
FailFormOpen <none>
FailFormUpdate <none>
FailMove <none>
FailSelChange <none>
GotFocus GotFocus
KeyDown KeyDown
KeyUp KeyUp
LostFocus LostFocus
MouseDblDown <none> (Closest event is DblClick)
MouseDown MouseDown
MouseUp MouseUp
RequestCollapse <none> (Closest event is Collapse)
RequestExpand <none> (Closest event is Expand)
RequestFormOpen <none>
RequestFormUpdate <none>
RequestHelp <none>
RequestMove <none>
RequestSelChange <none>
Updated Updated
<none> AfterLabelEdit
<none> BeforeLabelEdit
<none> MouseMove
<none> OLECompleteDrag
<none> OLEDragDrop
<none> OLEDragOver
<none> OLEGiveFeedback
<none> OLESetData
<none> OLEStartDrag
1 ABC Company
27 A Second Company
4 XYZ Company
427 Northwind Traders
You can compensate for this limitation by writing code to test the width
of each field, and then pad with an appropriate number of spaces to
properly align the columns.Additional query words: ADT
Keywords: kbfaq kbhowto kbprogramming kbusage KB162523