Thanks. This demo is very nice except that it doesn't show nor does it
explain how to dynamically add or delete nodes in the tree. From what I
understand I would have to rebuild the index -- how can it be done ?
Chris
I copied this code out of my application that views "Folders" in treeview
control "xTree."
The key I am using is like "a 1", "a 2" etc, so you see why
(Recordset)!Parent is set to Mid(xTree.SelectedItem.Key, 3)
This code adds a new record to the Folders table, calls it "New Folder," and
adds a new child node to the selected node in the treeview control to
display it:
------------------------------------------------
Private Sub cmdNewFolder_Click()
On Error GoTo Err_cmdNewFolder_Click
If xTree.SelectedItem Is Nothing Then Exit Sub
With CurrentDb.OpenRecordset("Folders")
.AddNew
!Foldername = "New Folder"
!Parent = Mid(xTree.SelectedItem.Key, 3)
.Update
.MoveLast
xTree.Nodes.Add xTree.SelectedItem, tvwChild, "a " & !FolderID,
!Foldername
xTree.SelectedItem.Expanded = True
End With
Exit_cmdNewFolder_Click:
Exit Sub
Err_cmdNewFolder_Click:
MsgBox Err.Description
Resume Exit_cmdNewFolder_Click
End Sub
------------------------------------------------
This sub removes a node, and deletes the corresponding record from the
Folder table:
------------------------------------------------
Private Sub cmdDeleteFolder_Click()
If xTree.SelectedItem Is Nothing Or _
DCount(1, "Reports", "[Parent]='" & xTree.SelectedItem.Key & "'") >
0 Or _
xTree.SelectedItem.Children > 0 Then Exit Sub
' Delete the record from the table
With CurrentDb.OpenRecordset("Folders", dbOpenDynaset)
.FindFirst "FolderID=" & Mid(xTree.SelectedItem.Key, 3)
.Delete
End With
xTree.Nodes.Remove xTree.SelectedItem.Index
End Sub
------------------------------------------------
Another example of an update you may want to perform. My code assumes there
will be only one user in the database editing folder names, so I didn't work
in any error-trapping with the NoMatch method:
------------------------------------------------
Private Sub xTree_AfterLabelEdit(Cancel As Integer, newstring As String)
With CurrentDb.OpenRecordset("folders", dbOpenDynaset)
.FindFirst "[FolderID] = " & Mid(xTree.Object.SelectedItem.Key, 3) '
use NoMatch for multiple user environment
.Edit
.Fields!Foldername = newstring
.Update
End With
End Sub
------------------------------------------------
Maybe these will be of some help.
Paul Johnson
> Thanks. This demo is very nice except that it doesn't show nor does it
> explain how to dynamically add or delete nodes in the tree. From what I
[quoted text clipped - 22 lines]
> > >
> > > Chris
chris - 19 Apr 2004 22:25 GMT
Thanks a lot Paul, I'll try that. I'll post back the results.
> I copied this code out of my application that views "Folders" in treeview
> control "xTree."
[quoted text clipped - 92 lines]
> > > >
> > > > Chris