UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Treeview Troubles - Oledragdrop..., Access 2016    
 
   
adaytay
post Aug 10 2018, 01:28 PM
Post#1


UtterAccess VIP
Posts: 3,734
Joined: 7-October 03
From: Yorkshire, England.


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.
Go to the top of the page
 
ChiliDog
post Aug 10 2018, 02:23 PM
Post#2



Posts: 194
Joined: 9-September 11



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


Go to the top of the page
 
adaytay
post Aug 11 2018, 04:47 AM
Post#3


UtterAccess VIP
Posts: 3,734
Joined: 7-October 03
From: Yorkshire, England.


Already done. oTree exists, but I can't use the selecteditem
Go to the top of the page
 
ChiliDog
post Aug 14 2018, 10:04 AM
Post#4



Posts: 194
Joined: 9-September 11



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.

Attached File(s)
Attached File  Form_frmEntityTree_01_Main.zip ( 4.35K )Number of downloads: 8
 
Go to the top of the page
 
adaytay
post Aug 15 2018, 02:37 PM
Post#5


UtterAccess VIP
Posts: 3,734
Joined: 7-October 03
From: Yorkshire, England.


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
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    15th October 2018 - 03:56 AM