I am building a treeview using DAO, and because there are many levels and many records, it is taking sometime (~5-10 seconds). I switched to ADO to se if there would be a performance boost and there was not.
My guess is that the fault lies not with ADO, but rather my opening and closing of the ADODB>Recordset everytime. Below is my code, please let me know if I am missing a key thing when working with ADO.
Thanks. Sean.
My guess is that the fault lies not with ADO, but rather my opening and closing of the ADODB>Recordset everytime. Below is my code, please let me know if I am missing a key thing when working with ADO.
Thanks. Sean.
Code:
Function tvwFillTree(strQuery1, strQuery2, strQuery3, strQuery4, strQuery5 As String)
On Error GoTo ErrorHandler
Dim strMessage As String
Dim db As String
Dim conn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim intVBMsg As Integer
Dim nod As Object
Dim strNode1Text As String
Dim strNode2Text As String
Dim strVisibleText1 As String
Dim strVisibleText2 As String
Dim ctltree As Control
'Dim NodeA As Node
Dim objImageList As Object
'Compose the database name
db = CurrentProject.Path & "\Raytheon Cost.mdb"
'Connect to the database
Set conn = New ADODB.Connection
conn.Mode = adModeRead
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & db
conn.Open
Set ctltree = Me.TV1
Set objImageList = Me.IL1
ctltree.ImageList = objImageList.Object
ctltree.SetFocus
ctltree.Nodes.Clear
Set rst = New ADODB.Recordset
Set rst.ActiveConnection = conn
rst.CursorType = adOpenDynamic
rst.Source = strQuery1
rst.LockType = adLockOptimistic
rst.Open
If rst.RecordCount = 0 Then
rst.Close
Set rst = Nothing
GoTo ErrorHandlerExit
End If
With ctltree
'Fill Level 1
'Set rst = New ADODB.Recordset
'Set rst.ActiveConnection = conn
'rst.CursorType = adOpenDynamic
'rst.Source = strQuery1
'rst.LockType = adLockOptimistic
'rst.Open
'Add a node object for each record in the first table/query.
'For parent nodes, the Key property is based on the the level of the tree
'in which the node exists and the Link Master field(s) you selected when
'linking levels in the wizard. For child nodes, the Relative property
'is based on the level of the tree in which the Parent node exists and
'the Link Child field(s) you selected when linking levels in the wizard.
Do Until rst.EOF
Debug.Print "Adding Level 1 item: " & rst![Parent]
strNode1Text = StrConv("Level1 -" & rst![Parent], _
vbLowerCase)
Debug.Print "Node 1 text: " & strNode1Text
strVisibleText1 = "PO: " & rst![ParentName]
Debug.Print "Level 1 visible text: " & strVisibleText1
Set nod = .Nodes.Add(Key:=strNode1Text, _
Text:=strVisibleText1, Image:="Closed")
nod.Expanded = False
rst.MoveNext
Loop
rst.Close
'Fill Level 2
Set rst = New ADODB.Recordset
Set rst.ActiveConnection = conn
rst.CursorType = adOpenDynamic
rst.Source = strQuery2
rst.LockType = adLockOptimistic
rst.Open
Do Until rst.EOF
Debug.Print "Adding Level 2 item: " & rst![Child]
strNode1Text = StrConv("Level1 -" & rst![Parent], vbLowerCase)
Debug.Print "Node 1 text: "; strNode1Text
strNode2Text = StrConv("Level2 -" & rst![ChildKey], vbLowerCase) '& " =" _
& rst![Child], vbLowerCase)
Debug.Print "Node 2 text: " & strNode2Text
strVisibleText2 = "DO: " & rst![Child]
Debug.Print "Visible text: " & strVisibleText2
.Nodes.Add relative:=strNode1Text, _
relationship:=tvwChild, _
Key:=strNode2Text, _
Text:=strVisibleText2, Image:="Case"
rst.MoveNext
Loop
rst.Close
'Fill Level 3
Set rst = New ADODB.Recordset
Set rst.ActiveConnection = conn
rst.CursorType = adOpenDynamic
rst.Source = strQuery3
rst.LockType = adLockOptimistic
rst.Open
Do Until rst.EOF
Debug.Print "Adding Level 3 item: " & rst![Child]
strNode1Text = StrConv("Level2 -" & rst![Parent], vbLowerCase)
Debug.Print "Node 1 text: "; strNode1Text
strNode2Text = StrConv("Level3 -" & rst![ChildKey], vbLowerCase) '& " =" _
& rst![Child], vbLowerCase)
Debug.Print "Node 2 text: " & strNode2Text
strVisibleText2 = "Ship: " & rst![Child]
Debug.Print "Visible text: " & strVisibleText2
.Nodes.Add relative:=strNode1Text, _
relationship:=tvwChild, _
Key:=strNode2Text, _
Text:=strVisibleText2, Image:="Ship"
rst.MoveNext
Loop
rst.Close
'Fill Level 4
Set rst = New ADODB.Recordset
Set rst.ActiveConnection = conn
rst.CursorType = adOpenDynamic
rst.Source = strQuery4
rst.LockType = adLockOptimistic
rst.Open
Do Until rst.EOF
Debug.Print "Adding Level 4 item: " & rst![Child]
strNode1Text = StrConv("Level3 -" & rst![Parent], vbLowerCase)
Debug.Print "Node 1 text: "; strNode1Text
strNode2Text = StrConv("Level4 -" & rst![ChildKey], vbLowerCase) '& " =" _
& rst![Child], vbLowerCase)
Debug.Print "Node 2 text: " & strNode2Text
strVisibleText2 = "Task: " & rst![Child]
Debug.Print "Visible text: " & strVisibleText2
.Nodes.Add relative:=strNode1Text, _
relationship:=tvwChild, _
Key:=strNode2Text, _
Text:=strVisibleText2, Image:="Task"
rst.MoveNext
Loop
rst.Close
'Fill Level 5
Set rst = New ADODB.Recordset
Set rst.ActiveConnection = conn
rst.CursorType = adOpenDynamic
rst.Source = strQuery5
rst.LockType = adLockOptimistic
rst.Open
Do Until rst.EOF
Debug.Print "Adding Level 5 item: " & rst![Child]
strNode1Text = StrConv("Level4 -" & rst![Parent], vbLowerCase)
Debug.Print "Node 1 text: "; strNode1Text
strNode2Text = StrConv("Level5 -" & rst![ChildKey], vbLowerCase) '& " ="
'& rst![Parent], vbLowerCase)
Debug.Print "Node 2 text: " & strNode2Text
strVisibleText2 = rst![Child]
Debug.Print "Visible text: " & strVisibleText2
.Nodes.Add relative:=strNode1Text, _
relationship:=tvwChild, _
Key:=strNode2Text, _
Text:=strVisibleText2, Image:="Clock"
rst.MoveNext
Loop
rst.Close
End With
conn.Close
ErrorHandlerExit:
Exit Function
ErrorHandler:
Select Case Err.Number
Case 35601
'Element not found
strMessage = "Possible Causes: You selected a table/query" _
& " for a child level which does not correspond to a value" _
& " from its parent level."
intVBMsg = MsgBox(Error$ & strMessage, vbOKOnly + _
vbExclamation, "Run-time Error: " & Err.Number)
Case 35602
'Key is not unique in collection
strMessage = "Possible Causes: You entered a duplicate Part Number," _
& " but used a Part Name different than similar parts."
intVBMsg = MsgBox(strMessage, vbOKOnly + _
vbExclamation, "Run-time Error: " & Err.Number & " " & Error$)
Case Else
intVBMsg = MsgBox(Error$ & "@@", vbOKOnly + _
vbExclamation, "Run-time Error: " & Err.Number)
End Select
Resume ErrorHandlerExit
End Function