VBA中的TreeView-无法显示列值



我是VBA新手。我正在设计一个包含产品装配结构的数据库。装配具有不同的级别,这些级别与主产品相关联。像这样:树状结构

我在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, tvwChild, "'" & (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, tvwChild, "'" & (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, 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"))
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"))
End If

'Next Parent
rstParent.MoveNext
Loop

End Sub

我没有使用正确的子名称和父名称来打印数量列。

相关内容

  • 没有找到相关文章

最新更新