最好是数据库中有三个字段分别保存年月日 Public Sub AddNodes() Dim strkey As String Dim mNodes As MSComctlLib.Nodes Dim mNode1 As MSComctlLib.Node Dim mNode2 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 id,mc from lb order by ID" .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 = "o" & .Fields("ID") Set mNode1 = mNodes.Add(, , strkey, .Fields("mc") & "", 7, 7) '第二层循环 Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset
strsql = "select * from ws where mid=" & .Fields("id") rs.Open strsql, conn, adOpenStatic, adLockPessimistic For j = 1 To rs.RecordCount Key = "t" & rs.Fields("id") Set mNode2 = mNodes.Add(mNode1.Index, tvwChild, Key, rs.Fields("mc"), 7, 7) rs.MoveNext Next .MoveNext Next .Close End WithSet rstNodes = Nothing On Error GoTo 0 End Sub
数据库中 字段 日期(格式为YYYY-MM-DD) 为主键 Public Sub AddNodes() Dim strkey As String Dim mNodes As MSComctlLib.Nodes Dim 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 日期 from lb order by 日期" .LockType = adLockReadOnly .CursorType = adOpenForwardOnly .Open End WithmNodes.ClearWith rs1 If .EOF And .BOF Then Set rs1 = Nothing Exit Sub End If dim intyear as integer dim intmonth as integer dim intday as integer dim strParentkey as string intyear=0 intmonth=0 while not .eof if intyear!=year(format(.Fields("日期"),"YYYY-MM-DD"))) strkey = "o" & year(format(.Fields("日期"),"YYYY-MM-DD"))) intyear=year(format(.Fields("日期"),"YYYY-MM-DD")) strparentkey="" mNodes.Add , , strkey, intyear & "", 7, 7 intmonth=0 end if if intmonth!=month(format(.fields("日期"),"YYYY-MM-DD"))) then if strparentkey ="" then strparentkey=strkey strkey="m" & intyear & intmonth intmonth=month(format(.fields("日期"),"YYYY-MM-DD"))) mNodes.Add strparentkey,tvwchild , strkey, intyear & "", 7, 7 end if intday=day((format(.fields("日期"),"YYYY-MM-DD"))) strkey="d" & intyear & intmonth & intday mNodes.Add strparentkey,tvwchild , strkey, intyear & "", 7, 7 .MoveNext wend .Close End WithSet rs1 = Nothing On Error GoTo 0 End Sub
Option ExplicitPrivate Sub Command1_Click() Dim i As Long Dim j As Long Dim n As Long
For i = 1998 To 2002 DoEvents For j = 1 To 12 For n = 1 To 31 If IsDate(Str(i) & "-" & Str(j) & "-" & Str(n)) Then Debug.Print Str(i) & "年" Debug.Print Str(j) & "月" Debug.Print Str(n) & "日" Debug.Print "=================" End If Next Next Next
Public Sub AddNodes()
Dim strkey As String
Dim mNodes As MSComctlLib.Nodes
Dim mNode1 As MSComctlLib.Node
Dim mNode2 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 id,mc from lb order by ID"
.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 = "o" & .Fields("ID")
Set mNode1 = mNodes.Add(, , strkey, .Fields("mc") & "", 7, 7)
'第二层循环
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
strsql = "select * from ws where mid=" & .Fields("id")
rs.Open strsql, conn, adOpenStatic, adLockPessimistic
For j = 1 To rs.RecordCount
Key = "t" & rs.Fields("id")
Set mNode2 = mNodes.Add(mNode1.Index, tvwChild, Key, rs.Fields("mc"), 7, 7)
rs.MoveNext
Next
.MoveNext
Next
.Close
End WithSet rstNodes = Nothing
On Error GoTo 0
End Sub
数据库中 字段 日期(格式为YYYY-MM-DD) 为主键
Public Sub AddNodes()
Dim strkey As String
Dim mNodes As MSComctlLib.Nodes
Dim 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 日期 from lb order by 日期"
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End WithmNodes.ClearWith rs1
If .EOF And .BOF Then
Set rs1 = Nothing
Exit Sub
End If
dim intyear as integer
dim intmonth as integer
dim intday as integer
dim strParentkey as string
intyear=0
intmonth=0
while not .eof
if intyear!=year(format(.Fields("日期"),"YYYY-MM-DD")))
strkey = "o" & year(format(.Fields("日期"),"YYYY-MM-DD")))
intyear=year(format(.Fields("日期"),"YYYY-MM-DD"))
strparentkey=""
mNodes.Add , , strkey, intyear & "", 7, 7
intmonth=0
end if
if intmonth!=month(format(.fields("日期"),"YYYY-MM-DD"))) then
if strparentkey ="" then strparentkey=strkey
strkey="m" & intyear & intmonth
intmonth=month(format(.fields("日期"),"YYYY-MM-DD")))
mNodes.Add strparentkey,tvwchild , strkey, intyear & "", 7, 7
end if
intday=day((format(.fields("日期"),"YYYY-MM-DD")))
strkey="d" & intyear & intmonth & intday
mNodes.Add strparentkey,tvwchild , strkey, intyear & "", 7, 7
.MoveNext
wend
.Close
End WithSet rs1 = Nothing
On Error GoTo 0
End Sub
Dim i As Long
Dim j As Long
Dim n As Long
For i = 1998 To 2002
DoEvents
For j = 1 To 12
For n = 1 To 31
If IsDate(Str(i) & "-" & Str(j) & "-" & Str(n)) Then
Debug.Print Str(i) & "年"
Debug.Print Str(j) & "月"
Debug.Print Str(n) & "日"
Debug.Print "================="
End If
Next
Next
Next
End Sub