How to: Let users edit the tree
Let users rename, add, delete, move, and copy nodes, and check each change before it's written to the data source.
Requires WKL Forms Object Library 1.23 or later.
Solution overview
Four properties turn the operations on: LabelEdit (rename), AllowAdditions (add),
AllowDeletions (delete), and AllowDragDrop (move and copy). All four are off by default
because they change data. With AutoSave = True (the default), each operation is written in a
single transaction; BeforeUpdate occurs before each write and can cancel the whole operation.
Implementation
Set them on the Other tab of the property sheet or in Form_Load:
With Me!Tree1.Object
.LabelEdit = True ' F2 or click on the selected node
.AllowAdditions = True ' Insert: same level; Ctrl+Insert: below
.AllowDeletions = True ' Delete, with confirmation
.AllowDragDrop = True ' drag to move, Ctrl+drag to copy; Ctrl+X/C/V, Alt+Arrow keys
End With
Check the changes in the form module:
Private Sub Tree1_BeforeUpdate(ByVal Key As Variant, ByVal Action As WklUpdateAction, Cancel As Integer)
If Action = wuaDelete And Not CurrentUserMayDelete() Then Cancel = True ' rolls back the whole operation
End Sub
Private Sub Tree1_AfterLabelEdit(ByVal Key As Variant, NewText As String, Cancel As Integer)
NewText = Trim$(NewText)
If Len(NewText) = 0 Then Cancel = True
End Sub
Private Sub Tree1_BeforeNodeAdd(ByVal ParentKey As Variant, NewKey As Variant, Text As String, Cancel As Integer)
Text = "New region"
End Sub
CurrentUserMayDelete is a placeholder for your own permission check.
Remarks
- The shortcut menu offers an operation only if its property allows it.
- Deleting a node removes its whole subtree;
BeforeDeletecan skip the confirmation (Confirm = False) or cancel the deletion. - New keys: Access assigns AutoNumber values. For other key fields, the control proposes a key that
BeforeNodeAddandBeforeNodeCopycan change: numbers continue from the highest key, text keys get a suffix in the UI language (A1-copy,A1-copy2, …), and GUID keys get a new GUID. - Attachment and multivalued fields aren't copied.
- Dragging works only within the same control.
- In a tree loaded from a
Recordsetor a file, changes affect only the display.
Related topics
en/wiki/msaccess/comctl/how-to/let-users-edit-the-tree.txt · Last modified: by Wolfgang van der Stille