Isn't it wierd ?!!! Here is my code:
___________________________________________
Sub AddMyTree()
On Error GoTo Err_AddMyTree
Dim conn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim strSQL As String
Dim nodCurrent As Node
Dim objTree As Object
Set objTree = Me.TreeView0
Set conn = CurrentProject.Connection
strSQL = "SELECT DISTINCT DateRec FROM Table1 GROUP BY DateRec"
rst.Open strSQL, conn, adOpenStatic, adLockReadOnly
Do While Not rst.EOF
Set nodCurrent = objTree.Nodes.Add(, , "a" & rst("DateRec"),
rst("DateRec"), 1, 2)
nodCurrent.Tag = rst("DateRec")
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Set conn = Nothing
Exit_AddMyTree:
Exit Sub
Err_AddMyTree:
Set rst = Nothing
Set conn = Nothing
MsgBox Err.Description, vbCritical, "AddMyTree"
Resume Exit_AddMyTree
End Sub
Private Sub Detail_Click()
End Sub
Private Sub TreeView0_NodeClick(ByVal Node As Object)
Dim strSQL As String
strSQL1 = "SELECT * FROM Table1 "
strSQL = strSQL1 & "WHERE DateRec = # " & CDate(Node.Tag) & " # "
Me.Table1_subform.Form.RecordSource = strSQL
Me.Table1_subform.Form.Requery
End Sub
Private Sub Form_Load()
AddMyTree
End Sub
__________________________________________
Can any body tell me what may be the problem ? I been struglling this for
days .
and
if possible can any one give me an example on how to make the treeview to be
displayed on a "year","Month","date" ,three levels grade, currently it just
displayed all the dates in the same manner
Many thanks !
Something like:
Dim nodeYear As Node
Dim nodeYear As Node
Dim nodeYearMonth As Node
Do While Not rst.EOF
Set nodeYear = Nothing
On Error Resume Next
Set nodeYear = objTree.Nodes _
("a" & Format(rst("DateRec").Value, "yyyy"))
On Error GoTo Err_AddMyTree
If nodeYear Is Nothing Then
Set nodeYear = objTree.Nodes _
.Add(, , "a" & Format(rst("DateRec").Value, "yyyy"), _
Format(rst("DateRec").Value, "yyyy"), 1, 2)
End If
Set nodeYearMonth = Nothing
On Error Resume Next
Set nodeYearMonth = objTree.Nodes _
("a" & Format(rst("DateRec").Value, "yyyymm"))
On Error GoTo Err_AddMyTree
If nodeYearMonth Is Nothing Then
Set nodeYearMonth = objTree.Nodes _
.Add(nodeYear, tvwChild, _
"a" & Format(rst("DateRec").Value, "yyyymm"), _
Format(rst("DateRec").Value, "mmmm"), 1, 2)
End If
Set nodCurrent = objTree.Nodes _
.Add(nodeYearMonth, tvwChild, _
"a" & rst("DateRec").Value, _
rst("DateRec").Value, 1, 2)
nodCurrent.Tag = rst("DateRec")
rst.MoveNext
Loop
Jamie.
--
Well, I didn't like trying to add parent nodes on each iteration. Could
be better to get a hierarchical recordset to begin with and only add
the parent nodes when needed e.g.
Set conn = New ADODB.Connection
conn.ConnectionString = _
"Provider=MSDataShape;Data " & _
CurrentProject.Connection.ConnectionString
conn.Open
strSQL = _
"SHAPE {SELECT DISTINCT YEAR(DateRec) AS" & _
" DateRecYear FROM Table1} AS chapRecDateYears" & _
" APPEND ((SHAPE {SELECT DISTINCT YEAR(DateRec)" & _
" AS DateRecYear, MONTH(DateRec) AS DateRecMonth" & _
" FROM Table1} APPEND ({SELECT DateRec, YEAR(DateRec)" & _
" AS DateRecYear, MONTH(DateRec) AS DateRecMonth" & _
" FROM Table1} AS chapRecDate RELATE DateRecYear" & _
" TO DateRecYear, DateRecMonth TO DateRecMonth))" & _
" AS chapRecDateYearsMonths RELATE DateRecYear" & _
" TO DateRecYear)"
rst.Open strSQL, conn, adOpenStatic, adLockReadOnly
Dim nodeYear As Node
Dim nodeYearMonth As Node
With rst
Do While Not .EOF
Set nodeYear = objTree.Nodes.Add( _
, , "a" & .Fields("DateRecYear").Value, _
.Fields("DateRecYear").Value, 1, 2)
With .Fields("chapRecDateYearsMonths").Value
Do While Not .EOF
Set nodeYearMonth = objTree.Nodes.Add( _
nodeYear, tvwChild, _
"a" & .Fields("DateRecYear").Value & _
.Fields("DateRecMonth").Value, _
.Fields("DateRecMonth").Value, 1, 2)
With .Fields("chapRecDate").Value
Do While Not .EOF
Set nodCurrent = objTree.Nodes.Add( _
nodeYearMonth, tvwChild, _
"a" & .Fields("DateRec").Value, _
.Fields("DateRec").Value, 1, 2)
.MoveNext
Loop
End With
.MoveNext
Loop
End With
.MoveNext
Loop
End With
Jamie.
--
Well, I didn't like trying to add parent nodes on each iteration. Could
be better to get a hierarchical recordset to begin with and only add
the parent nodes when needed e.g.
Set conn = New ADODB.Connection
conn.ConnectionString = _
"Provider=MSDataShape;Data " & _
CurrentProject.Connection.ConnectionString
conn.Open
strSQL = _
"SHAPE {SELECT DISTINCT YEAR(DateRec) AS" & _
" DateRecYear FROM Table1} AS chapRecDateYears" & _
" APPEND ((SHAPE {SELECT DISTINCT YEAR(DateRec)" & _
" AS DateRecYear, MONTH(DateRec) AS DateRecMonth" & _
" FROM Table1} APPEND ({SELECT DateRec, YEAR(DateRec)" & _
" AS DateRecYear, MONTH(DateRec) AS DateRecMonth" & _
" FROM Table1} AS chapRecDate RELATE DateRecYear" & _
" TO DateRecYear, DateRecMonth TO DateRecMonth))" & _
" AS chapRecDateYearsMonths RELATE DateRecYear" & _
" TO DateRecYear)"
rst.Open strSQL, conn, adOpenStatic, adLockReadOnly
Dim nodeYear As Node
Dim nodeYearMonth As Node
With rst
Click even to make it possible to be displayed on a subform
Private Sub TreeView0_NodeClick(ByVal Node As Object)
Dim strSQL As String
strSQL1 = "SELECT * FROM Table1 "
strSQL = strSQL1 & "WHERE DateRec = # " & CDate(Node.Tag) & " # "
Me.Table1_subform.Form.RecordSource = strSQL
Me.Table1_subform.Form.Requery
End Sub
this won't work please help
Private Sub TreeView0_NodeClick(ByVal Node As Object)
Dim strSQL As String
strSQL1 = "SELECT * FROM Table1 "
strSQL = strSQL1 & "WHERE DateRec = # " & CDate(Node.Tag) & " # "
Me.Table1_subform.Form.RecordSource = strSQL
Me.Table1_subform.Form.Requery
End Sub
This just not woking,please help
Oops! I forgot to set the tags. After the line:
Set nodCurrent = objTree.Nodes.Add(...)
add something like:
nodCurrent.Tag = .Fields("DateRec").Value
In the _Click event, remember to check that you are at the appropriate
level in the tree to use the tag as a date. It may be an idea to also
set the tags at the nodeYearMonth and nodeYearMonth levels
respectively.
Jamie.
--
"Private Sub TreeView0_NodeClick(ByVal Node As Object)
Dim strSQL As String
strSQL = "SELECT * FROM FinalQuery "
strSQL = strSQL & "WHERE Daterec = #" & CDate(Node.Tag) & "# "
Me.FinalQuery_subform.Form.RecordSource = strSQL
Me.FinalQuery_subform.Form.Requery
End Sub"
OK so you're not using the hierarchical example. The equivalent line
is:
nodCurrent.Tag = rst.Fields("DateRec").Value
> Please also tell me how to write the click event, I coy that code form
> aomewhere else. I have no idea of that at all.
I'm not a Forms expert but from memory you select the treeview, right
click, choose 'Build event' from the context menu which should take you
to Visual Basic Editor, with the control name selected in the dropdown
on the left (top left of the code pane), choose Click in the dropdown
on the right.
Jamie.
--
Sub AddMyTree()
On Error GoTo Err_AddMyTree
Dim conn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim strSQL As String
Dim nodCurrent As Node
Dim objTree As Object
Set objTree = Me.TreeView0
Set conn = CurrentProject.Connection
strSQL = "SELECT DISTINCT DateRec FROM FinalQuery GROUP BY DateRec;"
rst.Open strSQL, conn, adOpenStatic, adLockReadOnly
Dim nodeYear As Node
Dim nodeYearMonth As Node
Do While Not rst.EOF
nodCurrent.Tag = rst.Fields("DateRec").Value
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Set conn = Nothing
Exit_AddMyTree:
Exit Sub
Err_AddMyTree:
Set rst = Nothing
Set conn = Nothing
MsgBox Err.Description, vbCritical, "AddMyTree"
Resume Exit_AddMyTree
End Sub
Private Sub Detail_Click()
End Sub
Private Sub TreeView0_NodeClick(ByVal Node As Object)
Dim strSQL As String
strSQL = "SELECT * FROM FinalQuery "
strSQL = strSQL & "WHERE Daterec = #" & CDate(Node.Tag) & "# "
Me.FinalQuery_subform.Form.RecordSource = strSQL
Me.FinalQuery_subform.Form.Requery
End Sub
Private Sub Form_Load()
AddMyTree
End Sub
_____________________________________
You must isolate which part of which line is causing the error. My
guess would be
CDate(Node.Tag)
Put a breakpoint in the event and in the Immediate Window try:
? CDate(Node.Tag)
It works OK for me but then I tested with Table1.DateRec being a
DATETIME column ;-) Even so I think I'd be happier writing the tag in
ISO format e.g.
nodCurrent.Tag = Format$(rst.Fields("DateRec").Value, "yyyy-mm-dd
hh:nn:ss")
Jamie.
--
That's what I have
________
Set nodCurrent = objTree.Nodes _
.Add(nodeYearMonth, tvwChild, _
"a" & rst("DateRec").Value, _
rst("DateRec").Value, 1, 2)
nodCurrent.Tag = Format$(rst.Fields("DateRec").Value, "yyyy-mm-dd ')
___________
____
Private Sub TreeView0_NodeClick(ByVal Node As Object)
Dim strSQL As String
strSQL = "SELECT * FROM FinalQuery "
strSQL = strSQL & "WHERE Daterec = #" & CDate(Node.Tag) & "# "
Me.FinalQuery_subform.Form.RecordSource = strSQL
Me.FinalQuery_subform.Form.Requery
End Sub
_____
Jamie, would you be kind enough to paste a full code ? I almost dead of it