VBA中的TreeView-无法显示列值

问题描述

我是VBA的新手。我正在设计一个包含产品装配结构的数据库。装配体具有链接到主产品的不同级别。像这样: TreeStructure

我在MS Access Table中有一个树形结构,我想制作一个包含父和子名称以及它们各自数量的树形形式:Tree

我正在使用一些上线的代码,但无法通过它进行操作。 这是代码

    enter code here Private Sub cmdCollapse_Click()
    Dim n As Node
    For Each n In Me.GroupTree.Nodes
        If n.Expanded Then n.Expanded = False
    Next n
End Sub

Private Sub cmdExpand_Click()
    
    On Error GoTo ErrMsg
    
    Dim n As Node
    
    Application.Echo False
    Me.Painting = False
    
StartOver:
    
    For Each n In Me.GroupTree.Nodes
        If Not n.Expanded Then n.Expanded = True
    Next n

ExitHere:
    Application.Echo True
    Me.Painting = True
    Me.Repaint
    Exit Sub

ErrMsg:
    If Err.Number = 35606 Then
        '35606 - Control's collection has been modified
        '(because we are populating the nodes as they are expanded)
        cmdExpand_Click
    Else
        Debug.Print "cmdExpand_Click(): " & Err.Number & " - " & Err.Description
        MsgBox "Error occured in cmdExpand_Click(); " & Err.Number & " - " & Err.Description,vbCritical
    End If

End Sub

Private Sub cmdReloadTree_Click()
    'Clear any existing nodes
    Me.GroupTree.Nodes.Clear
    'Load top parent nodes
    GroupTree_LoadParents
End Sub

Private Sub Form_Open(Cancel As Integer)
    'Clear any existing nodes
    Me.GroupTree.Nodes.Clear
    'Load top parent nodes
    GroupTree_LoadParents
End Sub


Private Sub GroupTree_LoadParents()

    Dim nParent As Node,nChild As Node
    Dim db As DAO.Database
    Dim rstParent As DAO.Recordset
    Dim rstChild As DAO.Recordset
    Dim strsql As String
     
    Me![GroupTree].Linestyle = 1    'Root lines
    Me![GroupTree].Style = 7        'TreelinesPlusMinusPictureText
     
    Set db = CurrentDb()
    strsql = "SELECT * FROM tblGroup WHERE ParentGroup is Null"
    Set rstParent = db.OpenRecordset(strsql,dbOpenDynaset)
     
    
    'Populate Top Parents group
    do while Not rstParent.EOF
        'Add node
        Set nParent = GroupTree.Nodes.Add(,"'" & (rstParent.Fields("GroupID")),rstParent.Fields("Group") & rstParent.Fields("Quantity"))
        nParent.EnsureVisible
        
        'Check if there are child nodes
        strsql = "SELECT * FROM tblGroup WHERE ParentGroup = " & rstParent.Fields("GroupID")
        Set rstChild = db.OpenRecordset(strsql,dbOpenDynaset)
        If Not (rstChild.BOF And rstChild.EOF) Then
            'Add 1st child node but don't prepopulate the remaining children;
            'Those will be populated when the user expands a particular parent node
            Set nChild = GroupTree.Nodes.Add(nParent,tvwChild,"'" & rstChild.Fields("GroupID"),rstChild.Fields("Group") & rstParent.Fields("Quantity"))
        End If
        
        'Next Parent
        rstParent.MoveNext
    Loop
    
    
End Sub


Private Sub GroupTree_Expand(ByVal Node As Object)
    Dim nParent As Node,nChild As Node,nGrandhild As Node
    Dim db As DAO.Database
    Dim rstParent As DAO.Recordset
    Dim rstChild As DAO.Recordset
    Dim rstGrandchild As DAO.Recordset
    Dim strsql As String
     
    Application.Echo False
    Me.Painting = False
    
    With Me.GroupTree
        .Linestyle = 1    'Root lines (0 = No lines)
        .Style = 7        'TreelinesPlusMinusPictureText
        '.Style = 5       'no plus
    End With
     
    Debug.Print "Node.Key = " & Node.Key
    Debug.Print "Node.Text = " & Node.Text
     
    Set db = CurrentDb()
    strsql = "SELECT * FROM tblGroup WHERE GroupID = " & Right(Node.Key,Len(Node.Key) - 1)
    Set rstParent = db.OpenRecordset(strsql,dbOpenDynaset)
    
    'Add node
    Set nParent = GroupTree.Nodes("'" & rstParent.Fields("GroupID"))
    
    'Delete any existing child nodes
    Do Until nParent.Children = 0
        GroupTree.Nodes.Remove (nParent.Child.Index)
    Loop
    
    'Get child nodes
    strsql = "SELECT * FROM tblGroup WHERE ParentGroup = " & rstParent.Fields("GroupID")
    Set rstChild = db.OpenRecordset(strsql,dbOpenDynaset)

    'Loop through children
    While Not rstChild.EOF
        'Add child node
        Set nChild = GroupTree.Nodes.Add(nParent,"'" & (rstChild.Fields("GroupID")),rstChild.Fields("Group"))
        
        'Get grandchildren nodes
        strsql = "SELECT * FROM tblGroup WHERE ParentGroup = " & rstChild.Fields("GroupID")
        Set rstGrandchild = db.OpenRecordset(strsql,dbOpenDynaset)
        
        'Add placeholder grandchild
        If Not rstGrandchild.EOF Then
            'Add 1st grancchild node,but don't populate the rest
            Set nGrandhild = GroupTree.Nodes.Add(nChild,"'" & (rstGrandchild.Fields("GroupID")),rstGrandchild.Fields("Group"))
        End If
        
        'Next child
        rstChild.MoveNext
    Wend
    
    Application.Echo True
    Me.Painting = True
    Me.Repaint

End Sub

Private Sub GroupTree_LoadALL()

    Dim nParent As Node,rstParent.Fields("Group"))
        nParent.EnsureVisible
        
        'Check if there are child nodes
        strsql = "SELECT * FROM tblGroup WHERE ParentGroup = " & rstParent.Fields("GroupID")
        Set rstChild = db.OpenRecordset(strsql,rstChild.Fields("Group"))
        End If
        
        'Next Parent
        rstParent.MoveNext
    Loop
    
End Sub

解决方法

让它正常工作,我没有使用正确的子代和父代名称来打印数量列。