我这样做不知道行不行,你参考一下吧!
Dim conn As ADODB.Connection
Dim rst As ADODB.Recordset
Private Sub Form_Load()
Dim strSz() As String
Dim strDyc As String
Dim strDec As String
Dim j As Integer
Set conn = New ADODB.Connection
Set rst = New ADODB.Recordset
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb;Persist Security Info=False"
rst.Open "select * from tree", conn, adOpenKeyset, adLockReadOnly
If rst.RecordCount > 0 Then
j = 1
While Not rst.EOF
If Mid(rst.Fields(1), 3, 1) = "|" Then
strSz = Split(rst.Fields(1), "|") '给这个字段以“|”分这数组
If j = 1 Then
For i = 0 To UBound(strSz)
If i = 0 Then
Set Node = TreeView1.Nodes.Add(, , strSz(i), strSz(i))
Else
Set Node = TreeView1.Nodes.Add(strSz(i - 1), tvwChild, strSz(i), strSz(i))
End If
Next i
Else
For i = 0 To UBound(strSz)
strDyc = strSz(i)
If bleBl(strDyc) Then
GoTo 1 '跳转1位置
Else
If i = 0 Then
Set Node = TreeView1.Nodes.Add(, , strSz(i), strSz(i))
Else
Set Node = TreeView1.Nodes.Add(strSz(i - 1), tvwChild, strSz(i), strSz(i))
End If
End If
1:
Next i
End If
j = j + 1
Else
Set Node = TreeView1.Nodes.Add(, , rst.Fields(1) & rst.Fields(1), rst.Fields(1))
End If
rst.MoveNext
Wend
End If
End Sub
Private Function bleBl(bl As String) As Boolean
Dim i As Integer
For i = 1 To TreeView1.Nodes.Count
If TreeView1.Nodes(i).Text = bl Then bleBl = True
Next i
End Function
Dim mynod As Node
Set mynod = TreeView1.Nodes.Add(, , "a", "劳保")
Set mynod = TreeView1.Nodes.Add("a", tvwChild, , "鞋类")
Set mynod = TreeView1.Nodes.Add("a", tvwChild, , "木头")
mynod.EnsureVisible