Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Rearrange TreeView Nodes by Drag and Drop

Introduction.

I hope you found, last week’s tutorial on ImageCombo Control  useful for your Microsoft Access Projects.  With the TreeView ImageCombo Control we could create a beautiful Drop-down Menu, with several options, and accommodate it in a small space on the form. 

Likewise, in an earlier session we have learned how to Add new Nodes in a specific location on the Nodes hierarchy or Delete a Node and add a new one to relocate a Node on the Tree View Control. 

This method demands to create new record in the Source Table for the new Node.  Or delete an existing record and create a new one to relocate an existing Node to make these changes permanent.  In a way, with the use of Add/Delete Functions we could add new Nodes or re-arrange the existing Nodes on the TreeView Control. 

As far as re-arranging Nodes is concerned we have a better way to do it,  rather than deleting Nodes and re-creating them. Drag the Node from it’s current location and Drop it where we want it to be on the TreeView Control.  This is what we are going to learn in this episode.

This simple approach needs only updating the change on the ParentID field value of related record, to make the change permanent.

The Topics Covered so far in Earlier Sessions.

  1. Microsoft TreeView Control Tutorial
  2. Creating Access Menu with TreeView Control
  3. Assigning Images to TreeView Control
  4. Assigning Images to TreeView Control-2
  5. TreeView Control Check-Mark Add Delete Nodes
  6. TreeView ImageCombo Drop-down Access Menu

But, we may face some challenges while using this method and we will come to that little later in this Session.

Demo Data Table and Form.

We need a Table and a Form.  We already have a suitable table with the name Sample created in an earlier tutorial Session. If you have already downloaded the Demo Database, from the second link Page given above earlier, then you may use that Database for this Session also.  We will use the following objects from that Database for Drag-Drop experiments:

  • Table : Sample
  • Form: frmSample

The TreeView Control Image on frmSample with demo data is given below for reference:

You may download the Demo Database (ProjectMenu.zip) from the Second Link given above and extract the ProjectMenu.accdb Database.

New Form for Drag Drop Trial Runs.

  1. Open the ProjectMenu.accdb Database.

  2. Make a Copy of the table Sample and name it as Sample_bk for safe keep, we will need it’s original data without change later.  When we experiment with the drag drop method it is necessary to update the ParentId field value on the Sample demo Table.   But, we need the original data later, without these changes.

  3. Create a new Form with the name frmDragDrop.

  4. The frmDragDrop Form Design will look like the image given below, when you finish with it.

  5. Insert the TreeView Control from the ActiveX Controls’ List and place it on the form, leaving enough space above the control, so that we can create two Command Buttons and a Heading Label above it.  Drag the sizing handle at the bottom right-corner to make it large enough to display all the Nodes, without scrolling.

  6. Change the Name Property value of TreeView Control to TreeView0.

  7. Insert a Command Button above and left edge of the TreeView Control.  Change it’s Name Property Value to cmdExpand and Caption value to Expand All.

  8. Insert a second Command Button above and to the right edge of the TreeView Control.  Change it’s Name Property Value to cmdCollapse and the Caption Property Value to Collapse All.
  9. Insert a Label Control above the Command Buttons, wide enough to write the Heading as shown above and change it’s Font Size to 14.

  10. Ignore the ImageList Control for the time being, I have commented out the Code Lines that modifies the Node ImageList index numbers.  Later on you can Import the ImageList Control with manually uploaded Images, from our earlier Tutorial Demo Database (from the 4th Link Page given above) and use it to display Node-Images on Nodes.  When Node positions are changed during Drag Drop actions we need to change Node Images also depending on the Node’s position (Root-level Node or Child-Node) on the TreeView Control.

    Drag Drop Form Module Code.

  11. Display the VBA Code Module of the Form frmDragDrop, Copy and Paste the following VBA Code (this is only the first half of the Form Module Code)  into the frmDragDrop Form’s Class Module and Save the Form:

    Option Compare Database
    Option Explicit
    
    Dim tv As MSComctlLib.TreeView
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim imgListObj As MSComctlLib.ImageList
    Const KeyPrfx As String = "X"
    
    Private Sub Form_Open(Cancel As Integer)
    Set tv = Me.TreeView0.Object
    
    'Set imgListObj = Me.ImageList1.Object
    'tv.ImageList = imgListObj
    
    LoadTreeView
    
    End Sub
    
    Sub LoadTreeView()
    Dim strKey As String
    Dim strPKey As String
    Dim strText As String
    Dim strsQL As String
    
    strsQL = "SELECT * FROM Sample ORDER BY ID"
    
    Set db = CurrentDb
    Set rst = db.OpenRecordset(strsQL, dbOpenDynaset)
        
    tv.Nodes.Clear
    
    'Add all Items are added as Root Nodes
    Do While Not rst.BOF And Not rst.EOF
        strKey = KeyPrfx & CStr(rst!ID)
        strText = rst!desc
        tv.Nodes.Add , , strKey, strText
        
        'With tv.Nodes.Item(strKey)
        '    .Image = 1
        '    .SelectedImage = 4
        'End With
    
        rst.MoveNext
    Loop
    
    'Prepare to update the Parent-Key of Nodes
    'wherever applicable to move and position the Child Nodes
    strPKey = ""
    rst.MoveFirst
    Do While Not rst.EOF
        strPKey = Nz(rst!parentid, "")
        
        If Len(strPKey) > 0 Then
            strPKey = KeyPrfx & strPKey
            strKey = KeyPrfx & CStr(rst!ID)
            strText = rst!desc
            
            'Move the Child Node under it's Parent-Node
            Set tv.Nodes.Item(strKey).Parent = tv.Nodes.Item(strPKey)
            
    'Update Image and SelectedImage Properties
    'with ImageList Index numbers
            'With tv.Nodes.Item(strKey)
            '    .Image = 2
            '    .SelectedImage = 3
            'End With
    
        End If
        rst.MoveNext
    Loop
    
    rst.Close
    Set rst = Nothing
    Set db = Nothing
    
    End Sub
    
    
    Private Sub TreeView0_NodeClick(ByVal Node As Object)
    Dim SelectionNode As MSComctlLib.Node
        
    'Ensure that the clicked node equals the selected node in the tree
    If Not Node Is Nothing Then
        Set SelectionNode = Node
           If SelectionNode.Expanded = True Then
                SelectionNode.Expanded = False
            Else
                SelectionNode.Expanded = True
            End If
    End If
    End Sub
    
    Private Sub cmdCollapse_Click()
    Dim tmpnod As MSComctlLib.Node
    For Each tmpnod In tv.Nodes
        If tmpnod.Expanded = True Then
            tmpnod.Expanded = False
        End If
    Next
    
    End Sub
    
    Private Sub cmdExpand_Click()
    Dim tmpnod As MSComctlLib.Node
    For Each tmpnod In tv.Nodes
        If tmpnod.Expanded = False Then
            tmpnod.Expanded = True
        End If
    Next
    
    End Sub

    I know you are familiar with the above Code, if you have already gone through the earlier Episodes, except the LoadTreeView() Sub-routine with some changes. Here, populating the TreeView Nodes are divided into a two step process. 

    Briefly this is what happens in this Subroutine.

    • All the records on the Sample Table are loaded as Root-level Nodes of the TreeView Control, with ID field value as Key, first.

    • Again these records are read a second time and checked for the presense of ParentId field values, if empty then the Node will be retained as Root-level Node.

    • If ParentID field have a Value then identify the Node with the ParentID value as Node-Key and move the current Node as it’s Child Node, or it’s [Relative]  Parameter (of Add() Methodvalue gets updated.

      Even though it appears that the two step Node populating procedure is an unnecessary exercise, there is a good reason why we have to follow this method. We will come back to this little later and you will know it without much explaining.

    • On the Design of the Form I have given an ImageList Control. You may insert the ImageList ActiveX Control and upload some images in it manually from the disk, or copy and paste this control with images from earlier Demo Database Downloads. In either case please ensure that the name of the ImageList control is ImageList1. Otherwise you need to change the name in the Code.

    • After that enable the commented-out lines in Form_Open() Event Procedure.  Enable the following lines, by removing the comment symbol from the beginning of the line:

      'Set imgListObj = Me.ImageList1.Object
      'tv.ImageList = imgListObj
      

    • In the TreeView0_OLEDragDrop() Subroutine (in the 2nd part of the VBA Code) enable the Image Index Parameters of Nodes, by removing the Comment symbols from those lines too.  With these changes the Node Images will appear on the TreeView.  If you have an ImageList Control of your own with uploaded Images then change the Index Numbers based on what Image you would like to insert on the Nodes.

      The TreeView0_NodeClick() Event Procedure Expands the current Node, if the Child-Nodes are in collapsed state, otherwise child Nodes will be Collapsed. Normally this action is controlled (without Code) by clicking on the +/- Symbol on the Tree-line of Node with Child Nodes.

      The Subroutines cmdExpand_Click() and cmdCollapse_Click() Events Expands all Nodes and collapse all Nodes respectively.

      When the above Code Runs the display looks like the Form View Image given below:

    • You may save the frmDragDrop Form and open it in Normal View.  If everything went on well then you will see the above screen.  Try out the Expand All and Collapse All Command Buttons and check whether they are working too.  If not then re-check the following settings are correct or not:

    • i)  TreeView Control’s Name is: TreeView0

    • ii)  Display the Property Sheet of Exampand All Command Button and select [Event Procedure] in the On Click Event Property.

    • iii) Ensure that the same setting is intact for Collapse All Command Button also. 

    • iv) Click on a Node with Child Nodes to see whether they collapse or expand on repeated clicks.

    •  v) If ImageList Control is placed on the Form then it’s name must be ImageList1.

      Let us proceed with the second part of the VBA Code that implements the Drag-Drop Events.

    Second Half of the VBA Code.

  12. Copy the following Second Part of the VBA Code, on the frmDragDrop Form Module, that implements the Drag-Drop action, and Paste it below the existing Code:

    Private Sub TreeView0_OLEStartDrag(Data As Object, AllowedEffects As Long)
        Set Me.TreeView0.SelectedItem = Nothing
    End Sub
    
    
    Private Sub TreeView0_OLEDragOver(Data As Object, _
                                    Effect As Long, _
                                    Button As Integer, _
                                    Shift As Integer, _
                                    x As Single, _
                                    y As Single, _
                                    State As Integer)
        
        Dim SelectedNode As MSComctlLib.Node
        Dim nodOver As MSComctlLib.Node
        
        If tv.SelectedItem Is Nothing Then
            'Select a node if one is not selected
            Set SelectedNode = tv.HitTest(x, y)
            If Not SelectedNode Is Nothing Then
                SelectedNode.Selected = True
            End If
        Else
            If tv.HitTest(x, y) Is Nothing Then
            'do nothing
            Else
                'Highlight the node the mouse is over
                Set nodOver = tv.HitTest(x, y)
                Set tv.DropHighlight = nodOver
            End If
        End If
    
    End Sub
    
    
    Private Sub TreeView0_OLEDragDrop(Data As Object, _
                                        Effect As Long, _
                                        Button As Integer, _
                                        Shift As Integer, _
                                        x As Single, _
                                        y As Single)
    
        Dim sourceNode As MSComctlLib.Node
        Dim SourceParentNode As MSComctlLib.Node
        Dim targetNode As MSComctlLib.Node
        
        Dim tmpRootNode As MSComctlLib.Node
        Dim strtmpNodKey As String
        Dim ChildNode As MSComctlLib.Node
        
        Dim strSPKey As String
        Dim strTargetKey As String
        
        Dim strsQL As String
        Dim intKey As Integer
        Dim intPKey As Integer
        
        On Error Resume Next
        
        Select Case Screen.ActiveControl.Name
                
               Case TreeView0.Name
                    Set sourceNode = tv.SelectedItem
                
        End Select
        
        'Get Source Parent Node & Target Node Reference
        Set SourceParentNode = sourceNode.Parent
        Set targetNode = tv.HitTest(x, y)
                
        'If any errors then exit
        If Err <> 0 Then
            MsgBox Err & " : " & Err.Description, vbInformation + vbCritical, "OLEDragDrop()"
            Err.Clear
            Exit Sub
        Else
            On Error GoTo 0
        End If
        
    
        'Get/define Source parent Node Key to compare it with Target Node Key
        If SourceParentNode Is Nothing Then
            strSPKey = "Empty"
        Else
            strSPKey = SourceParentNode.Key
        End If
        
        'Check the Target Node/Location and define the Key
         Select Case True
            Case targetNode Is Nothing
                strTargetKey = "Empty"
            
            Case targetNode.Key = ""
                strTargetKey = "Empty"
                Set targetNode = Nothing
            Case Else
                strTargetKey = targetNode.Key
         End Select
        
        'Make sure the Target Node is not the source Node's own parent
        If strTargetKey = strSPKey Then Exit Sub
        
        'Track User's Node move action, check for error.
        On Error Resume Next
        
        If targetNode Is Nothing Then
            
            'If target Node is Nothing (the Node dropped in the empty area),
            'then the Node must be moved to the Root-level
            'save the original sourceNode.Key
            strtmpNodKey = sourceNode.Key
            
            'Modify the source Node Key, with addition of some text, say 'Empty', like 'X5Empty'
            'So that a temporary Node can be created with the original source Node key.
            'Note: Two Nodes with the same Key cannot remain in memory at the same time.
            'The Source Node with key 'X5Empty' deleted later,
            'temporary Node takes it's droped location.
            sourceNode.Key = sourceNode.Key & strTargetKey
    
            'Create the temporary Root Node, with original sourceNode Key
            Set tmpRootNode = tv.Nodes.Add(, , strtmpNodKey, sourceNode.Text)
            
            'define the Root Node image indexes
            'With tmpRootNode
            '    .Image = 1
            '    .SelectedImage = 4
            'End With
            
            'Move all child Nodes from SourceNode,if any,
            'as tmpRootNode's Children
            Do Until sourceNode.Children = 0
                Set sourceNode.Child.Parent = tmpRootNode
                
                'modify Node image indexes
                'With sourceNode
                '    .Image = 2
                '    .SelectedImage = 3
                'End With
            Loop
    
            'Delete the Source Node with modified Key from TreeView
            tv.Nodes.Remove sourceNode.Index
            
            'Move the tmpRootNode with original Key
            'to the dropped location on TreeView
            Set sourceNode = tmpRootNode
        Else
            'Move the sourceNode under targetNode as child
            Set sourceNode.Parent = targetNode
            
            'modify Node image indexes
            'With sourceNode
            '    .Image = 2
            '    .SelectedImage = 3
            'End With
        End If
        
        'Notify, if there was an Error then Exit, else Update PrentID of related Record.
        If Err <> 0 Then
            MsgBox Err & " : " & "Unable to move:" & vbCrLf & Err.Description, vbInformation + vbCritical, "DragDrop2()"
            Exit Sub
        Else
            'Build and execute the SQL statement to update the record
            If targetNode Is Nothing Then
                intKey = Val(Mid(sourceNode.Key, 2))
                strsQL = "UPDATE Sample SET ParentID = Null" & _
                         " WHERE ID = " & intKey
            Else
                intKey = Val(Mid(sourceNode.Key, 2))
                intPKey = Val(Mid(targetNode.Key, 2))
                
                strsQL = "UPDATE sample SET ParentID = " & intPKey & _
                         " WHERE ID = " & intKey
            End If
            
            'Modify the table records
            CurrentDb.Execute strsQL, dbFailOnError
            
            'If an error raised then refresh TreeView and exit
            If Err <> 0 Then
                MsgBox Err & " : " & Err.Description
                LoadTreeView 'Refresh/display TreeView without changes
            Else
                'Sort Nodes
                If sourceNode.Parent Is Nothing Then
                    sourceNode.Root.Sorted = True
                Else
                    sourceNode.Parent.Sorted = True
                End If
                
                tv.Nodes(sourceNode.Key).Selected = True
            End If
        End If
        On Error GoTo 0
    
    End Sub
    
    Private Sub TreeView0_OLECompleteDrag(Effect As Long)
    
        'Turn off the drophighlight
        Set tv.DropHighlight = Nothing
    
    End Sub
    
    Private Sub Form_Close()
    
    Set tv = Nothing
    End Sub

For the Drag-Drop action there are four Sub-routines, they are executed automatically when you Drag the Node(s), highlights the Node when moved over other Nodes, and finally drops it on a different Node or on the Root-level empty area.

The Main Subroutines of the Code.

  • TreeView0_OLEStartDrag()  - Initializes the selected item and sets the Node to Nothing
  • TreeView0_OLEDragOver()  - Works like the Mouse Move Event, highlights the Node, when drags a Node over it on the way to the Target Node.
  • TreeView0_OLEDragDrop() – Performs check and controls, positions the Node(s) in the Dropped Location and updates the record on Base Table.
  • TreeView0_OLECompleteDrag()  - The DropHighlight Property is set to Nothing.

We can do the Drag and Drop work with the TreeView0_OLEDragDrop() Subroutine alone.  In that case there will not be any Node-highlight, when the Source Node moves over other Nodes, from one location to the other, except that the Mouse Pointer changes to dragging a second arrow behind it, like in the sample image given below:

So, we will pay our attension on this subroutine and check the Code in detail from the beginning.  At the beginning of the subroutine we have declared the necessary Nodes and String Variables and others. 

Instead of repeating line-by-line analyses here, I have Commented each line/section of codes suitably so that you will understand what it does, when you go through the Code.  You may go through them.

The Sequence of Drap Drop Events

Let us understand the sequence of Events, the User Selects a Node, Drags over other Nodes on the way to it’s final destination and Drop it on the Target Node.  Or Drop it on the empty area on the TreeView Control, with the  intention to make it as a Root-level Node. 

When you drag a Node over another Node-Text the Node-Text gets highlighted, saying that your current position is on this Node on the way, wherever you are going from here. When moved out of the Node-text the highlight disappears.  This happens all through to the Target Node.  The TreeView0_OLEDragOver() Subroutine takes care of this highlighting action.

When you Drop a Node somewhere the TreeView0_OLEDragDrop() Subroutine takes over charge. Here, we have to analyse the User’s intentions and take appropriate Action.  The following information must be saved and analysed to move the Node to the correct location.

The Important Information to Keep Track Of.

  1. The Source Node Reference, Node Key and ParentID Values, Node Children, if any.

  2. The Target Node or location Reference, Node Key.

  3. If  the Target is not a Node but the empty area of the TreeView Control, then the Source Node to be moved into the root-level position.

  4. The Source Node when Droped on another Node, the Target Node become the new Parent  of the Source Node.

  5. If Source Node have Children of there own then they also must be moved with their Parent.

  6. ** When the Node is dragged and dropped on it’s own Parent-Node then Ignore this action.

    ** For example, check the above image.  If we drag the TextBox Node and drop it on it’s parent-node Controls, or drags the Controls Node and drop it on it’s Parent Node Form then those moves will be ignored.

  7. ** If Root-level Node is dragged and dropped into the empty area, then no action will be taken because it is already a Root-level Node.

For all valid moves of Node we need to update the ParentID field value of related record on the Sample Table.

Node Drop in the Root-level Empty Area.

In the case of item number 3 above we have to create a Root level Node, with the same ID Number of the Source Node, which is not allowed.  Duplicate Key Value not allowed in the TreeView hierarchy. This is the only area of the Code, where you will find little confusing on the procedure followed there.

The procedure is as given below:

  1. Modify the existing TreeView Node Key with the addition of some extra text, (say Key X5 change to X5Empty), to avoid Key conflicts, while creating temporary Node with the original Key.
  2. Create a temporary Node with the Original Key: X5.
  3. Move all Children Nodes from the Source Node, if any, as children to the Temporary Node.
  4. Delete the TreeView Source Node with the modified Key: X5Empty from the TreeView Control, but the related record on the Sample table is not touched.
  5. Move the temporary Node with the Original Key X5 with it’s children to the  Root-level position of the TreeView Control.
  6. Update the ParentID field of the related record with a zero length String (“”)  to mark it as a Root-level Node.

Self Experiments of Drag Drop.

You may try out some Drag and Drop experiments yourself and see how it works.  Select a Node, click and hold the left mouse button, drag the Node and drop it on another Node, or drop it in an empty area on the TreeView Control. When you drag the Node over other Node-Text it is highlighted and when you are out of the Node the highlight goes out. The dragged Node will appear in the new location where you have dropped it.  You may repeat this drag drop experiments by selecting single Node or Node with Children. 

Based on this movement of Nodes the related record’s ParentID field value will be updated with the Key value (ID) of the Target Node related record.

Why Two Step Node Populating Procedure?

Now,  we will go back to the LoadTreeView() Subroutine, to take a second look on the two step process we have adopted for populating all Nodes to the TreeView Control. 

  • All records in the Sample Table are initially added as Root-level Nodes, using ID field value as Node-Key.

  • In the second pass of the records, if the ParentID field value is empty then that Node will remain as Root Node, without change.

  • All  other Node related records with ParentID Value are correctly moved under it’s Parent Node.

Naturally, the question comes up,  why we have to do it this way?

We will do a simple experiment to make the answer clear without explaining it in too many words.  You might have already done some trial runs of drag and drop trial runs yourself and rearranged Nodes, in the process updated those record’s ParentID values with the change. So, we need to reset the record values to it’s original state in the Sample Table, before we start a new demo.

We have already created a copy of our Table Sample earlier, with the name Sample_bk as a backup.  Delete the Sample Table and make a copy from Sample_bk with the original name: Sample.

Open the Table and view the records and their ParentID field Values.  The sample Image of the Table is given below:

The ID Field Values are AutoNumbers and they are all in sequential order and all Id value is unique.  The following simple Rule governs the addition of a Child Node to the TreeView Control. 

The Simple Child Node Rule: The ParentID field Value (Parent-Key) in a record expects that a Parent Node already exists in the TreeView Control, with the same value as Node-Key (the ID). 

Check the third record from top in the above Table Image.  The ParentID field value is 2 and the ID of the current record is 3.  In this case the record with ID 2 will be added to the TreeView Control before we attempt to add the third record to the Node.  Both records not necessarily be next to each other. Check the record with ID number 21, it’s ParentID field value is 12, less than the current records ID value 21. 

In either case when the program encounters the ParentID Value in a record it assumes that the record with the ID value equal to the ParentID was already added as a Node in the TreeView Control in the earlier cycle of populating the Nodes.

Justifying the two Step Procedure.

Let us try some Drag-Drop trial runs.  But, before that we have a Form with the name frmSample, we have used in the first Tutorial Session and in that we have loaded all the TreeView Nodes at one go.  Yes, we have followed the same method so far and we need some change from now on.  But, before that let us open the old form and see how the Nodes appear on the Form.

  1. Open the Form frmSample to see how the TreeView display looks like, with the Sample table records, loaded using the old rule.

  2. If you are through viewing the TreeView Nodes then close the Form.

  3. Now, Open frmDragDrop Form.  We are preparing to drag and drop a Node.

  4. Select the Node with the Node-Text Table, Click and hold the left mouse button, drag and drop it on the Node, with the Node-Text Form.

  5. The Table Node with it’s immediate Child-Node Fields and it’s Children Nodes are moved as Child Nodes under the Form Node.

  6. Close the Form frmDragDrop and open it again. The Nodes will appear correctly, where you have dropped it, like the image given below.

  7. Close the frmDragDrop Form.

  8. Now, open the Form frmSample to see how this change appears on this Form. You will be greeted with an Error message, Element Not Found with Error Number: 35601.

  9. Select the Debug Command Button to go to the highlighted Code line, where Error occured.

  10. Point the mouse on nodKey Param of Add() method, it shows X3, point the mouse on ParentKey parameter and it shows X7. 

    With these two information we can assume that we are on the record with the ID value 3 and trying to designate this Node as a child Node to a Node not populated into the TreeView Control yet, with ID value 7.

  11. Press F5 Key to bring up the same dialogbox again and Click the End Command Button to stop the Program and bring up the Form on the Database Window. Close the frmSample Form.

  12. Open the Sample Table to view the arrangement of the  ParentID Numbers, after our drag and drop action.  The Records will look like the Image given below and I have highlighted the record that triggered the Error with ParentID value and showing it’s Parent record position.

Following the earlier Node populating normal procedure, we are at the third record position.  Since, the records ParentID value 7,  the Nod with ID value 7 must be present in the TreeView Control.  The Node with ID Value 7 is not yet populated into the TreeView Control, but we are trying to reference the non-existent Node and this triggers an Error.

Even if you Sort the records on the ParentID field order the records new arrangement will look like the Image given below:

Now, some other record’s Parent Node is not in the expected position. 

Hence, in these circumstances our two step TreeView Nodes loading approach works for both normal and after drag drop actions as well.

In the first step populate all records as Root-level Nodes in the TreeView Control using ID field value as Node-Key.

Now, all the Nodes of all records are available in the TreeView Control. It will be easy to move them around wherever we want.  It doesn’t say any of the required Node doesn’t exists in the TreeView.

In the second pass on the same set of records, the records with empty ParentID field value are untouched and allowed to remain as Root-level Nodes.   In other cases moves the Node as Child-Node under it’s Parent Node, by updating the [Relative] Parameter of the Node with the following Statement:

Set tv.Nodes.Item(strKey).Parent = tv.Nodes.Item(strPKey)

This is what we do through the second  pass on the same set of records.  You may do it  by resetting the Record Pointer to the first record, by executing rst.MoveFirst before the Do . . . Loop .EOF condition  and rst.MoveNext to access each record as we normally do.

Second Step in Reverse Order.

Or you may do it in reverse order.  After populating all records as Root-level Nodes the Record Pointer will be beyond the last record and on the .EOF position.  You may reset the record pointer to the last record, by executing rst.MoveLast before the Do . . . Loop .BOF check, and execute rst.MovePrevious to access each record and move the Nodes correctly under it’s Parent Node.  But, the Nodes may load slightly different in placement order of Nodes.

You may try this out yourself with the above suggested change of Code and see the result.

Download Demo Database


  1. MS-Access Class Module and VBA
  2. MS-Access VBA Class Object Arrays
  3. MS-Access Base Class and Derived Objects
  4. VBA Base Class and Derived Object-2
  5. Base Class and Derived Object Variants
  6. MS-Access Recordset and Class Module
  7. Access Class Module and Wrapper Classes
  8. Wrapper Class Functionality Transformation

Share:

No comments:

Post a Comment

Comments subject to moderation before publishing.

MS-Access Tips on your Finger-Tip

  • Download Android App 'MSA Guru' Version of LEARN MS-ACCESS TIPS AND TRICKS from Google Play Store.

Want to Post Free Ads on the Web


Translate



PageRank
Subscribe in a reader
Your email address:

Delivered by FeedBurner

Search

Popular Posts

Blog Archive

Powered by Blogger.

Follow by Email

Labels

Forms Functions How Tos MS-Access Security Reports msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Class Module External Links Queries Array msaccess reports Accesstips WithEvents msaccess tips Downloads Objects Menus and Toolbars Collection Object MsaccessLinks Process Controls Art Work Property msaccess How Tos Combo Boxes Dictionary Object Graph Charts Query VBA msaccessQuery Calculation Event List Boxes Command Buttons Controls Data Emails and Alerts Form Custom Functions Custom Wizards DOS Commands Data Type Key Object Reference TreeView Control ms-access functions msaccess functions msaccess graphs msaccess reporttricks Command Button ImageList Control Report msaccess menus msaccessprocess security advanced Access Security Add Auto-Number Field Type Form Instances ImageList Item Macros Menus Nodes RaiseEvent Recordset Top Values Variables Wrapper Classes msaccess email progressmeter Access2007 Copy Excel Export Expression Fields Join ListView Control Methods Microsoft Numbering System Records Security Split SubForm Table Tables Time Difference Utility Workgroup database function msaccess wizards tutorial Access Emails and Alerts Access Fields Access How Tos Access Mail Merge Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Common Controls Conditional Formatting Data Filtering Database Records Defining Pages Diagram Disk Dynamic Lookup Error Handler External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Union Query User Users Water-Mark Word automatically commands hyperlinks iSeries Date iif ms-access msaccess msaccess alerts pdf files reference restore switch text toolbar updating upload vba code