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
Bookmarks