一个TreeView控件加载问题?

programfish 2003-12-14 08:03:46
我想在启动时加载Access数据库中一个表内的内容到TreeView控件中,数据库中的内容为年,月,日三列,TreeView控件中的排列为父结点为年,子结点为月,子子结点为日,像日记一样。遍历数据库中的年月日到一个三维数组中再加载入TreeView行不行,有其它更好的方法吗,请高手给个思路。
...全文
24 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
liberte 2003-12-14
遍历
回复
wumy_ld 2003-12-14
原理很简单,就是遍历了
回复
online 2003-12-14
以下代码经过测试

循环遍历,代码有些繁琐,但是容易读
数据表:mz
字段:id,n,m,r,memo
数据1,2003,12,13,xxx
2,2003,12,14,xxx
3,2003,12,15,xxx

Private Sub Form_Load()
Dim strkey As String
Dim mNodes As MSComctlLib.Nodes
Dim mNode1 As MSComctlLib.Node

Dim I As Long

On Error Resume Next

Set mNodes = tvwMain.Nodes
Dim rs1 As ADODB.Recordset
Set rs1 = New ADODB.Recordset
With rs1
Set .ActiveConnection = conn
.CursorLocation = adUseClient
.Source = "select n from mz group by n order by n"
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With

mNodes.Clear
With rs1
If .EOF And .BOF Then
Set rs1 = Nothing
Exit Sub
End If
For r = 1 To .RecordCount
strkey = "l" & .Fields("ID")
Set mNode1 = mNodes.Add(, , strkey, .Fields("n") & "")
'第二层循环
AddSecondNodes mNode1, .Fields("n")
.MoveNext
Next
.Close
End With

Set rs1 = Nothing
On Error GoTo 0
End Sub

Public Sub AddSecondNodes(mNode As MSComctlLib.Node, lID As String)
Dim strkey As String
Dim mNodes As MSComctlLib.Nodes
Dim mNode2 As MSComctlLib.Node

Dim I As Long

On Error Resume Next

Set mNodes = tvwMain.Nodes
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
With rs
Set .ActiveConnection = conn
.CursorLocation = adUseClient
.Source = "select m from mz where n='" & lID & "' group by n,m order by m"
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With

With rs
If .EOF And .BOF Then
Set rs = Nothing
Exit Sub
End If
For r = 1 To .RecordCount
Set mNode2 = mNodes.Add(mNode, tvwChild, , .Fields("m"))
AddthreeNodes mNode2, lID, .Fields("m")
.MoveNext
Next
.Close
End With

Set rs = Nothing
End Sub


Public Sub AddthreeNodes(mNode As MSComctlLib.Node, lID As String, m As String)
Dim strkey As String
Dim mNodes As MSComctlLib.Nodes
Dim mNode3 As MSComctlLib.Node

Dim I As Long

On Error Resume Next

Set mNodes = tvwMain.Nodes
Dim rs3 As ADODB.Recordset
Set rs3 = New ADODB.Recordset
With rs3
Set .ActiveConnection = conn
.CursorLocation = adUseClient
.Source = "select r from mz where n='" & lID & "' and m='" & m & "' order by r"
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With
'mNodes.Clear
With rs3
If .EOF And .BOF Then
Set rs3 = Nothing
Exit Sub
End If
For r = 1 To .RecordCount
'Key1 = "p" & .Fields("id")
Set mNode3 = mNodes.Add(mNode, tvwChild, , .Fields("r"))
.MoveNext
Next
.Close
End With

Set rs3 = Nothing
End Sub
回复
rainstormmaster 2003-12-14
没有好办法,只能遍厉
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7453

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告