Results 1 to 2 of 2

VBA EXCEL With Database Access - How To Delete TreeView Nodes

Threaded View

  1. #1
    Registered User
    Join Date
    01-03-2022
    Location
    Indonesia
    MS-Off Ver
    2013
    Posts
    2

    VBA EXCEL With Database Access - How To Delete TreeView Nodes

    Hi...
    I need help, I have a TreeView whose data is taken from an Access database (as shown in the picture). How do I delete a Node so that all Childs related to the selected Node are also deleted?
    For example, when I delete the Sumatera Barat Node Then Padang, Padang Utara and Air Tawar Barat Nodes are also deleted, along with the data in the database.

    Option Explicit
    
    Private conArea As New Connection, rsArea As New Recordset, strQry As String, ndeArea As Node
    Private StrKey1 As String, strKey2 As String, strText As String
    
    Private Sub OpenConnection()
         If conArea Is Nothing Then Set conArea = CreateObject("ADODB.Connection")
         conArea.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\Area.accdb"
    End Sub
    
    Private Sub CloseConnection()
         If Not conArea Is Nothing Then conArea.Close: Set conArea = Nothing
    End Sub
    
    Private Function GetNodes(vstrKey As String) As Node
         For Each ndeArea In tvw_Area.Nodes
              If ndeArea.Key = vstrKey Then
                   Set GetNodes = ndeArea
                   Exit Function
              End If
         Next
         Set GetNodes = Nothing
    End Function
    
    Private Sub ShowListView()
         strQry = "SELECT * FROM dArea ORDER BY AreaID;": rsArea.Open strQry, conArea: tvw_Area.Nodes.Clear
    
         Do While Not rsArea.EOF
              StrKey1 = "Key " & rsArea!AreaID: strKey2 = "Key " & rsArea!AreaSub: strText = rsArea!AreaName
              
              With tvw_Area.Nodes
                   If rsArea!AreaSub = 0 Then
                        .Add Key:=StrKey1, Text:=strText
                   Else
                        Set ndeArea = GetNodes(strKey2)
                        If Not ndeArea Is Nothing Then
                             Set ndeArea = .Add(strKey2, tvwChild, StrKey1, strText)
                        End If
                   End If
              End With
              rsArea.MoveNext
         Loop
         rsArea.Close
    End Sub
    
    Private Sub UserForm_Initialize()
         Call OpenConnection: Call ShowListView
    End Sub
    
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
         Call CloseConnection
    End Sub
    Attached Images Attached Images
    Last edited by Piscesto; Yesterday at 04:13 PM. Reason: I've got the solution

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Treeview - Add Child Nodes based on Sheet to Parent Node
    By judeprem in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-10-2022, 10:25 AM
  2. [SOLVED] Get a list of all selected child nodes of a treeview
    By ImranBhatti in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-29-2019, 01:24 AM
  3. [SOLVED] Transfer ALL nodes from a UserForm TreeView into a TreeView on a worksheet with VBA
    By Jerbinator in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-04-2015, 07:52 AM
  4. [SOLVED] Expand TreeView nodes
    By zplugger in forum Excel General
    Replies: 5
    Last Post: 01-08-2015, 07:57 AM
  5. [SOLVED] Error Whilst Updating Nodes On A Treeview
    By ajryan88 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-23-2013, 04:49 PM
  6. Delete record in access database with excel VBA
    By Eureka in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-14-2013, 02:15 PM
  7. Count Children of all nodes in a treeview
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-18-2010, 10:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1