Full Version: Treeview Troubles - Oledragdrop...
UtterAccess Forums > Microsoft® Access > Access Forms
adaytay
Hi guys, I'm trying to get a treeview to drag/drop but it's failing to work.

Here's the entire OLEDragDrop routine. I want this to allow drag/drop of the nodes anywhere in the tree. Scrolling also doesn't appear to work, but that's a whole other story.

The line that fails is If Not oTree.SelectedItem Is Nothing Then, with the error Onject variable or With block variable not set

CODE
Private Sub tvlocation_OLEDragDrop(Data As Object, Effect As Long, _
   Button As Integer, Shift As Integer, x As Single, y As Single)

On Error GoTo Errtvlocation_OLEDragDrop
Dim oTree As TreeView, strKey As String, strText As String, strParent As String, strSystem As String
Dim strImage As String
Dim nodNew As Node, nodDragged As Node, nodPrev As Node
Dim db As Database, rs As Recordset

Me.Form.TimerInterval = 0
Debug.Print "triggered drop"
'Exit Sub
Set db = CurrentDb
' Open the Employees table for editing.
Set rs = db.OpenRecordset(Me.cmbTreeview.Column(1), dbOpenDynaset, dbSeeChanges + dbFailOnError)
' Create a reference to the TreeView control.
Set oTree = Me!tvLocation.Object
' If nothing is selected for drag, do nothing.
    If blnDragNode Then
        If Not oTree.SelectedItem Is Nothing Then
            If MsgBox("Do you want to move this node to the selected location?", vbYesNo + vbQuestion + vbDefaultButton2, "Move Selected Node") = vbYes Then
                ' Reference the selected node as the one being dragged.
                Set nodDragged = oTree.SelectedItem
                rstDummy.FindFirst "id = " & GetIdFromNode(nodDragged)

                ' If the node was dragged to an empty space, update the relevant
                ' table and make this employee a root node.
                If oTree.DropHighlight Is Nothing Then
                    MsgBox "Cannot create a root level node"
                ' If you are not dropping the node on itself.
                ElseIf nodDragged.Index <> oTree.DropHighlight.Index Then
                    With oTree.DropHighlight
                        If Len(.FullPath) - Len(Replace(.FullPath, "|", "")) < 1 Then
                            ' At least 1 | so the node is at least 2 levels down
                            MsgBox "Location can only belong to a Subsystem or another Location"
                            Exit Sub
                        End If
                    End With

                    ' Set the drop target as the selected node's parent.
                    tvLocation_Expand oTree.DropHighlight
                    Set nodDragged.Parent = oTree.DropHighlight
                    ' Locate the record in the Employees table and update it.
                    With rs
                        .FindFirst "id=" & GetIdFromNode(nodDragged)
                        .Edit
                        .Fields("Parent") = GetIdFromNode(oTree.DropHighlight)
                        .Update
                    End With
                    tvLocation_NodeClick nodDragged
                End If
            End If
        End If
    End If

' Deselect the node
Set nodDragged = Nothing
' Unhighlight the nodes.
Set oTree.DropHighlight = Nothing
Exittvlocation_OLEDragDrop:
    On Error Resume Next
    rsNode.Close    ' If not Open this will cause an error, hence the error trapping setting above
    blnDragNode = False
    Exit Sub
Errtvlocation_OLEDragDrop:
    ' If you create a circular branch.
    If Err.Number = 35614 Then
        MsgBox "A Location cannot be a child of its own child.", _
            vbCritical, "Move Cancelled"
    Else
        MsgBox "An error occurred while trying to move the node. " & _
        "Please try again." & vbCrLf & Error.Description
    End If
    Resume Exittvlocation_OLEDragDrop
End Sub



Hoping some experts can take a look and help - Thanks.
ChiliDog
been a while since I've worked on treeview, much less the super-fussy drag & drop logic, but suggest setting breakpoints and stepping thru each line to make sure objects are defined as expected, especially lines

Set oTree = Me!tvLocation.Object

If Not oTree.SelectedItem Is Nothing Then


adaytay
Already done. oTree exists, but I can't use the selecteditem
ChiliDog
Attached is treeview drag/drop logic from a project I did long ago. Don't recall the nuances of drag/drop methods but did a quick test and it does work. The treeview shows ownership hierarchy amongst a bunch of related entities (ParentCorp owns sub1, sub2, sub3; sub1 owns sub4, sub5 and so on). The drag/drop is to drag an entity from current owner to new owner. Hopefully it is useful for you.
adaytay
Sorry for the delay, ChiliDog. Thanks for sharing your code btw, very generous. My code works it just can't see the treeview object. Having had a bad time with this years ago, I think it's obviously a mscomctl.ocx version issue, I've tried a few different versions but no joy as yet. Will keep looking...

My avatar has never been more apt wink.gif
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.