利用TreeView控件实现多级商品信息浏览

Dim i As Integer '定义整型变量
Public Sub tree_change() '定义添加树状列表的函数
Dim key, text, bh As String
'Dim nod As Node
Adodc1.RecordSource = "select * from 商品基础信息表 order by 商品编号"
Adodc1.Refresh
If Adodc1.Recordset.RecordCount > 0 Then
Adodc1.Recordset.MoveFirst
Do While Adodc1.Recordset.EOF = False
If Len(Trim(Adodc1.Recordset.Fields("商品编号"))) = 2 Then
key = Trim(Adodc1.Recordset.Fields("本级名称"))
text = "(" & Trim(Adodc1.Recordset.Fields("商品编号")) & ")" & Trim(Adodc1.Recordset.Fields("本级名称"))
Set Node1 = TreeView1.Nodes.Add(, , key, text, 1)
End If
If Len(Trim(Adodc1.Recordset.Fields("商品编号"))) = 4 Then
key = Trim(Adodc1.Recordset.Fields("本级名称"))
text = "(" & Trim(Adodc1.Recordset.Fields("商品编号")) & ")" & Trim(Adodc1.Recordset.Fields("本级名称"))
Set Node2 = TreeView1.Nodes.Add(Node1.Index, tvwChild, key, text, 2)
End If
If Len(Trim(Adodc1.Recordset.Fields("商品编号"))) = 7 Then
key = Trim(Adodc1.Recordset.Fields("本级名称"))
text = "(" & Trim(Adodc1.Recordset.Fields("商品编号")) & ")" & Trim(Adodc1.Recordset.Fields("本级名称"))
Set Node3 = TreeView1.Nodes.Add(Node2.Index, tvwChild, key, text, 3)
End If
Adodc1.Recordset.MoveNext
Loop
End If
End Sub
Private Sub Form_Activate()
'查询所有商品信息
Adodc1.RecordSource = "select * from 商品基础信息表 order by 商品级别,商品编号"
Adodc1.Refresh
With Adodc1.Recordset
If .RecordCount > 0 Then
For i = 0 To 7
If .Fields(i) <> "" Then Text1(i).text = .Fields(i)
Text1(i).Locked = True
Next i
End If
End With
Call tree_change '调用函数
End Sub
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
Text1(3).text = TreeView1.SelectedItem.key '赋值给text1(3).text
Adodc1.RecordSource = "select * from 商品基础信息表 where 本级名称='" + Trim(Text1(3).text) + "'order by 商品级别,商品编号"
Adodc1.Refresh
If Adodc1.Recordset.RecordCount <> 0 Then
For i = 0 To 7
Text1(i).text = ""
If Adodc1.Recordset.Fields(i) <> "" Then Text1(i).text = Trim(Adodc1.Recordset.Fields(i))
Next i
End If
End Sub
Private Sub CmdExit_Click()
End
End Sub

你可能感兴趣的:(treeview)