Public Sub WriteTree()
On Error GoTo Tree_Error
' *********************************************************
' subroutine writes out telephone info for the customers
' *********************************************************
' ADO variable declarations and sets
Dim conn As ADODB.Connection
Set conn = CurrentProject.Connection
Dim rs As ADODB.Recordset
Set rs = CreateObject("ADODB.Recordset")
' miscellaneous variable declarations
Dim tSQL As String: tSQL = ""
Dim treSQL As String
Dim sKeyString As String
Dim sDisplay As String
' -----------------------------------------------
' set up SQL string
tSQL = SELECT DISTINCT id, phone_num, cName FROM " _
& "tblLLFeature WHERE trans_date BETWEEN #" _
& dteStart & "# AND #" & dteEnd & "#;"
' -----------------------------------------------
' clear existing tree nodes
Me!treCust.Nodes.Clear
' -----------------------------------------------
' open recordset
rs.Open tSQL, conn
' *********************************************************
' the add method used below utilizes the following syntax
' <treeview control name>.Nodes.Add 1, 2, 3, 4 where:
' 1 = identifying key node back to the parent (relates to #3 of parent node); if parent, leave blank
' 2 = type of node being added ; blank = parent, tvwChild = child node.
' 3 = unique key name for this node - usually use primary key of recordset
' 4 = text description of node (what user sees)
' I haven't yet found anything good to describe this tree node thingy, so this
' is what I've come up with - seems to work
' -----------------------------------------------
' write parent nodes out to tree, using the phone number as the key value,
' and display the customer phone number and name as node text
While Not rs.EOF
' set node key string
sKeyString = CStr(rs(1))
' set node display text
sDisplay = rs(1) & " :: [ " & rs("cName") & " ]"
' add node
Me!treCust.Nodes.Add , , sKeyString, sDisplay
rs.MoveNext
Wend
rs.Close
' -----------------------------------------------
' get all feature transactions for each customer,
' and write to the tree (prefix: LLF)
treSQL = "SELECT id, trans_date, salesperson, " _
& "feature, order_num, ATScomm, phone_num, " _
& "ATSStatus, recNum FROM tblLLFeature WHERE " _
& "trans_date BETWEEN #" & dteStart & "# AND #" _
& dteEnd & "#;"
' open recordset
rs.Open treSQL, conn
' iterate recordset
While Not rs.EOF
' set node key string
sKeyString = "LLF," & rs(6) & "," & rs(0) & "," _
& rs(1) & "," & rs(2) & "," & rs(3) & "," _
& rs(5) & "," & rs(8)
' set node display text
sDisplay = "LLF: " & rs(1) & " -> " & rs(3) _
& " for: $" & CCur(rs(5))
' add node to tree
Me!treCust.Nodes.Add CStr(rs(6)), tvwChild, sKeyString, sDisplay
' move to next record
rs.MoveNext
Wend
rs.Close
End If
Tree_Exit:
' hourglass off
DoCmd.Hourglass False
' free up memory by releasing variables
tSQL = "": treSQL = "": sDisplay = "": sKeyString = ""
Exit Sub
Tree_Error:
Select Case Err.Number
Case 35601 ' element not found error
Resume Next
Case 35602 ' not unique key
Resume Next
Case Else
MsgBox "Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description & vbCrLf & vbCrLf _
& "Contact your database administrator for further assistance.", vbCritical, "STOP!"
End Select
DoCmd.Hourglass False
End Sub
like I said, this is just a sample piece of code from my Treeview control, but I hope it gives you an idea on how to use it ... maybe I should this as an FAQ?
Greg Tammi, IT Design & Consultation
Work: [URL unfurl="true"]http://www.atsbell.com[/URL]
Home: [URL unfurl="true"]http://members.shaw.ca/gregandliz[/URL]