在 MS Access 2010 中使用 VBA 添加第三层到 treeview active x 控件

Adding a third level to treeview active x control using VBA in MS Access 2010

我有以下 VBA 代码,它使用 parent table Groups 填充树视图控件,然后是 child table Categories,我还有一个child到categories,名字叫Section。我希望能够将第三层添加到我的树视图控件中,这样我就可以显示 section。但是,我不知道如何正确执行此操作。非常感谢有关此主题的任何帮助。

我正在尝试使用与 groupcategories 相同的方法来添加 child 级别;但到目前为止没有成功。

我不确定我的原始代码是否完全正确,但它似乎工作正常。

我使用的是 Microsoft treeview 控件 6.0 版,如果这有什么不同的话。

Private Sub Form_Load()
Dim nodX As Node
Dim MyDB As DAO.Database
Dim MyRS As DAO.Recordset
Dim MyRSChild As DAO.Recordset
Dim strSQL As String

Set MyDB = CurrentDb()
Set MyRS = MyDB.OpenRecordset("SELECT * FROM Prt_Group ORDER BY Group_Number", dbOpenDynaset)


Set nodX = Treeview1.Nodes.Add(, , , "Parts List Treeview")

'Populate grp Nodes
Do While Not MyRS.EOF
    Set nodX = Treeview1.Nodes.Add(1, tvwChild, "Group" & MyRS![PartGroupID], MyRS![Group_Number] & " - " & MyRS![Group_Description])
        nodX.EnsureVisible
    MyRS.MoveNext
 Loop

 strSQL = "Select * From Prt_Category ORDER BY PartCat_Number"
 Set MyRSChild = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)

 'Populate category Nodes
 Do While Not MyRSChild.EOF
   Set nodX = Treeview1.Nodes.Add("Group" & MyRSChild![PartGroupID], tvwChild, "A" & CStr(MyRSChild![PartCatID]), _
              " " & MyRSChild![PartCat_Number] & " - " & _
              MyRSChild![PartCat_Description])
   MyRSChild.MoveNext
 Loop

 MyRSChild.Close
 MyRS.Close
 Set MyRSChild = Nothing
 Set MyRS = Nothing

End Sub

没有足够的信息来提供完整的答案我只能帮你一点点:

 Private Sub Form_Load()
     '.....
     'Populate category Nodes
     Do While Not MyRSChild.EOF
         Set nodX = Treeview1.Nodes.Add("Group" & MyRSChild![PartGroupID], tvwChild, "A" & CStr(MyRSChild![PartCatID]), _
              " " & MyRSChild![PartCat_Number] & " - " & _
              MyRSChild![PartCat_Description])

         'insert third level node begins here
         'Maybe do a dataset operation on "Section table" and a while loop
          Treeview1.Nodes.Add nodx, tvwChild, "KEYOFSECTION", "LABELOFSECTION"

         MyRSChild.MoveNext
     Loop
 '...

End Sub

使用唯一键更改 "KEYOFSECTION" 并使用适当的部分标签更改 "LABELOFSECTION"

我使用了以下成功运行的代码。我不确定这是否是最有效的编码方式,但它确实有效。这 page 帮助我理解了节点对象。

Private Sub Form_Load()
Dim nodX As Node
Dim MyDB As DAO.Database
Dim MyRS As DAO.Recordset
Dim MyRSChild As DAO.Recordset
Dim strSQL As String

Set MyDB = CurrentDb()
Set MyRS = MyDB.OpenRecordset("SELECT * FROM Prt_Group ORDER BY Group_Number asc", dbOpenDynaset)


Set nodX = Treeview1.Nodes.Add(, , , "Groups/Categories/Sections Treeview")

'Populate grp Nodes
Do While Not MyRS.EOF
    Set nodX = Treeview1.Nodes.Add(1, tvwChild, "Group" & MyRS![PartGroupID], MyRS![Group_Number] & " - " & MyRS![Group_Description])
        nodX.EnsureVisible
    MyRS.MoveNext
 Loop

 strSQL = "Select * From Prt_Category ORDER BY PartCat_Number ASC"
 Set MyRSChild = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)

 'Populate category Nodes
 Do While Not MyRSChild.EOF
   Set nodX = Treeview1.Nodes.Add("Group" & MyRSChild![PartGroupID], tvwChild, "A" & CStr(MyRSChild![PartCatID]), _
              " " & MyRSChild![PartCat_Number] & " - " & _
              MyRSChild![PartCat_Description])
   MyRSChild.MoveNext
 Loop
'-------------------NEW CODE FOR THIRD LEVEL----------------------------    
Dim strSQL1 As String
Dim myRSChild1 As DAO.Recordset

strSQL1 = "Select * From Prt_Section ORDER BY Section_Number"
Set myRSChild1 = MyDB.OpenRecordset(strSQL1, dbOpenSnapshot)

Do While Not myRSChild1.EOF
   Set nodX = Treeview1.Nodes.Add("A" & CStr(myRSChild1![PartCatID]), tvwChild, "B" & CStr(myRSChild1![SectionID]), _
              " " & myRSChild1![Section_Number] & " - " & _
              myRSChild1![Section_Description])
   myRSChild1.MoveNext
 Loop
'------------- END OF ADDITIONAL CODE-----------------------    
 'Root Text Bold
 Treeview1.Nodes(1).Bold = True


 MyRSChild.Close
 MyRS.Close
 myRSChild1.Close
 Set MyRSChild = Nothing
 Set MyRS = Nothing
 Set myRSChild1 = Nothing
end sub