我想在启动时加载Access数据库中一个表内的内容到TreeView控件中,数据库中的内容为年,月,日三列,TreeView控件中的排列为父结点为年,子结点为月,子子结点为日,像日记一样。遍历数据库中的年月日到一个三维数组中再加载入TreeView行不行,有其它更好的方法吗,请高手给个思路。

解决方案 »

  1.   

    以下代码经过测试循环遍历,代码有些繁琐,但是容易读
    数据表:mz
    字段:id,n,m,r,memo
    数据1,2003,12,13,xxx
        2,2003,12,14,xxx
        3,2003,12,15,xxxPrivate Sub Form_Load() 
    Dim strkey As String
    Dim mNodes As MSComctlLib.Nodes
    Dim mNode1 As MSComctlLib.NodeDim I As LongOn Error Resume NextSet 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 WithmNodes.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 WithSet rs1 = Nothing
    On Error GoTo 0
    End SubPublic Sub AddSecondNodes(mNode As MSComctlLib.Node, lID As String)
    Dim strkey As String
    Dim mNodes As MSComctlLib.Nodes
    Dim mNode2 As MSComctlLib.NodeDim I As LongOn Error Resume NextSet 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 WithWith 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 WithSet 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.NodeDim I As LongOn Error Resume NextSet 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 WithSet rs3 = Nothing
    End Sub