Private Sub LoadMyTreeView()
On Error GoTo Err_Handler
Dim strSQL As String
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim nod As Node
Dim iNode As Integer
Dim strPName As String
Dim strPKey As String
Dim strSName As String
Dim strSKey As String
Dim strAName As String
Dim strAKey As String
‘ Clear the treeview nodes
tvwMyTreeView.Nodes.Clear
Set dbs = CurrentDb
strSQL = “SELECT pub_name, stor_name, au_name, pub_id, stor_id, au_id ” & _
“FROM qryPublisherStoreAuthor ORDER BY pub_name, stor_name;”
Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
If Not rst.BOF Then rst.MoveFirst
Do Until rst.EOF
‘ Add Publisher node
If strPName <> rst!pub_name Then
strPKey = rst!pub_id & “|”
strPName = rst!pub_name
Set nod = tvwMyTreeView.Nodes.Add(, , strPKey, strPName, “publisher_open”, “publisher_closed”)
strSName = “”
End If
‘ Add Store node
If strSName <> rst!stor_name Then
strSKey = strPKey & rst!stor_id & “|”
strSName = rst!stor_name
Set nod = tvwMyTreeView.Nodes.Add(strPKey, tvwChild, strSKey, strSName, “store”, “store”)
nod.Tag = rst!stor_id
strAName = “”
End If
‘ Add Author node
If strAName <> rst!au_name Then
strAKey = strSKey & rst!au_id & “|”
strAName = rst!au_name
Set nod = tvwMyTreeView.Nodes.Add(strSKey, tvwChild, strAKey, strAName, “author”, “author”)
nod.Tag = rst!au_id
End If
rst.MoveNext
Loop
‘ Initialize ListView to empty recordset.
LoadMyListView (“”)
Exit_Here:
Set dbs = Nothing
Set rst = Nothing
Exit Sub
Err_Handler:
MsgBox Err.Description, vbCritical, “ERROR”
Resume Next
End Sub
Private Sub tvwMyTreeView_NodeClick(ByVal Node As Object)
On Error GoTo Err_Handler
Dim lngStor_id As Long
Dim strAu_Id As String
‘ Grab the stor_id from the node’s tag property
‘ If null, set to zero.
strAu_Id = Nz(Node.Tag, 0)
‘ Given we have a non-zero ID, load the listview control
If strAu_Id <> “” Then
Call LoadMyListView(strAu_Id)
End If
Exit_Here:
Exit Sub
Err_Handler:
MsgBox Err.Description, vbCritical, “ERROR”
Resume Next
End Sub
Private Sub LoadMyListView(ByVal au_id As String)
On Error GoTo Err_Handler
Dim strSQL As String
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim lstItem As ListItem
Dim strItem As String
Dim strKey As String
Dim intCount As Integer
‘ Remove all existing items
While Me!lvwMyListView.ListItems.Count > 0
Me!lvwMyListView.ListItems.Remove (1)
Wend
‘open recordset of filtered Title/Authors
Set dbs = CurrentDb
strSQL = “SELECT title, title_id FROM qryTitleAuthor WHERE [au_id]='” & au_id & “‘”
Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
‘add each entry in the rs to the listview
If Not rst.BOF Then rst.MoveFirst
Do Until rst.EOF
strItem = rst!title
strKey = “ID=” & rst!title_id
Set lstItem = Me!lvwMyListView.ListItems.Add(, strKey, strItem)
intCount = intCount + 1
rst.MoveNext
Loop
If intCount > 0 Then Me!lvwMyListView.ListItems(1).Selected = True
Exit_Here:
Set dbs = Nothing
Set rst = Nothing
Exit Sub
Err_Handler:
MsgBox Err.Description, vbCritical, “ERROR”
Resume Next
End Sub
Private Sub lvwMyListView_ItemCheck(ByVal Item As Object)
On Error GoTo Err_Handler
Dim strSQL As String
Dim iCurr As Integer
Dim strKey As String
Dim lngKeyID As Long
For iCurr = 1 To Me!lvwMyListView.ListItems.Count
If Me!lvwMyListView.ListItems(iCurr) = Item Then
strKey = Me!lvwMyListView.ListItems(iCurr).Key
MsgBox strKey, vbInformation, “Note”
End If
Next
Exit_Here:
Exit Sub
Err_Handler:
MsgBox Err.Description, vbCritical, “ERROR”
Resume Next
End Sub