Before I have a question about "how to fill level 3 of treeview by query" but I did not get answer, after searching a lot in Google I read that there is a control wizard that works with Access97.
I like to know how I can use this wizard with Access 2007. That is possible?
What I have found for 2 levels is like below code for Function tvwBooks_Fill()
Design of tables and query is like below and I like to change below code for the treeview that
First level is: Author
Second level is: book
Third level is: transmittal no
------------------------------------------------------------------------------------
tblTransmittal (TransId (pk),TransmittalNo)
tblbook (Bookid (pk),title)
tblAuthor(AuthorID(pk), AuthorPrefix, AuthorFirstName, AuthorMiddleName, AuthorLastName, AuthorSuffix)
tblBookAuthors (Bookid(pk), AuthorID(pk))
tblTransmittal_Book_Author (Transid(pk), Bookid(pk), AuthorID(pk))
qryEBookAuthors:
SELECT DISTINCT tblAuthors.AuthorID, Trim(IIf([AuthorFirstName],[AuthorLastName] & IIf([AuthorPrefix],[AuthorPrefix] & " ","") & IIf([AuthorFirstName],", " & [AuthorFirstName],"") & IIf([AuthorMiddleName]," " & [AuthorMiddleName]),[AuthorLastName])) & IIf([AuthorSuffix]," " & [AuthorSuffix],"") AS LastNameFirst
FROM tblAuthors
ORDER BY Trim(IIf([AuthorFirstName],[AuthorLastName] & IIf([AuthorPrefix],[AuthorPrefix] & " ","") & IIf([AuthorFirstName],", " & [AuthorFirstName],"") & IIf([AuthorMiddleName]," " & [AuthorMiddleName]),[AuthorLastName])) & IIf([AuthorSuffix]," " & [AuthorSuffix],"");
qryEBooksByAuthor :
SELECT tblBookAuthors.AuthorID, tblBookAuthors.BookID, qryBookAuthors.LastNameFirst, tblBooks.Title, tblBooks.Type, Switch([ReadStatus]=3," " & Chr$(215),[ReadStatus]=2," " & Chr$(247)) AS BeenRead
FROM tblBooks INNER JOIN (tblBookAuthors INNER JOIN qryBookAuthors ON tblBookAuthors.AuthorID = qryBookAuthors.AuthorID) ON tblBooks.BookID = tblBookAuthors.BookID
ORDER BY qryBookAuthors.LastNameFirst, tblBooks.Title;
Code:
Function tvwBooks_Fill()
'Created by Helen Feddema 2-10-2002
'Last modified 4-23-2002
'============================================================
'Modified from a procedure generated by the Access 97
'Treeview Control Wizard
'PURPOSE: Fill the ActiveX Treeview Control 'tvwBooks' with
'author and book information
'ACCEPTS: Nothing
'RETURNS: Nothing
'CALLED FROM: Form Load event
'============================================================
On Error GoTo ErrorHandler
Dim strMessage As String
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim intVBMsg As Integer
Dim strQuery1 As String
Dim strQuery2 As String
Dim nod As Object
Dim strNode1Text As String
Dim strNode2Text As String
Dim strVisibleText As String
Set dbs = CurrentDb()
strQuery1 = "qryEBookAuthors"
strQuery2 = "qryEBooksByAuthor"
With Me![tvwBooks]
'Fill Level 1
Set rst = dbs.OpenRecordset(strQuery1, dbOpenForwardOnly)
'Add a node object for each record in the "qryEBookAuthors" table/query.
'The Key argument concatenates the level number and the LastNameFirst
'field of the Level 1 query, to create a unique key value for the node.
'The Text argument is the text displayed as a Level 1 node in the
'TreeView control
Do Until rst.EOF
strNode1Text = StrConv("Level1" & rst![LastNameFirst], _
vbLowerCase)
Set nod = .Nodes.Add(Key:=strNode1Text, _
Text:=rst![LastNameFirst])
'Expand the entire node
nod.Expanded = True
rst.MoveNext
Loop
rst.Close
'Fill Level 2
Set rst = dbs.OpenRecordset(strQuery2, dbOpenForwardOnly)
'Add a node object for each record in the "qryEBooksByAuthor"
'table/query.
'The value of the Relative argument matches the Key argument value
'for the Level 1 node this Level 2 node belongs to.
'The Relationship argument takes a named constant, tvwChild,
'indicating that the Level 2 node becomes a child node of the
'Level 1 node named in the Relative argument.
'The Key argument concatenates the level number and the Title
'field of the Level 2 query, to create a unique key value for the node.
'The Text argument is the text displayed as a Level 2 node in the
'TreeView control
Do Until rst.EOF
strNode1Text = StrConv("Level1" & rst![LastNameFirst], vbLowerCase)
strNode2Text = StrConv("Level2" & rst![Title], vbLowerCase)
strVisibleText = rst![Title]
.Nodes.Add relative:=strNode1Text, _
relationship:=tvwChild, _
Key:=strNode2Text, _
Text:=strVisibleText
rst.MoveNext
Loop
rst.Close
End With
dbs.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 selected a non-unique" _
& " field to link levels."
intVBMsg = MsgBox(Error$ & strMessage, vbOKOnly + _
vbExclamation, "Run-time Error: " & Err.Number)
Case Else
intVBMsg = MsgBox(Error$ & "@@", vbOKOnly + _
vbExclamation, "Run-time Error: " & Err.Number)
End Select
Resume ErrorHandlerExit
End Function