Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete tree view nodes

Status
Not open for further replies.

lingyi

Programmer
Apr 11, 2001
65
SG
hi,
Anyone know how to delete nodes from tree view control. Please show me some example of it. Thanks.
 
Hi!

Your question is too common. What do you want to remove? To remove one node or all nodes, or nodes group of some criterias, or...?

Examples:

1. Delete all nodes:

Me.trvTreeView.Nodes.Clear

2. Remove selected node with all them children:

dim lngIndex as long
dim nodX as node

set nodx = Me.trvTreeView.selecteditem
lngIndex = nodx.index

Me.trvTreeView.Nodes.Remove lngIndex


Aivars
 
Hi Aivars,
Look like u kind of familiar with tree view function. I got one more question. Is it possible for tree view to add the node directly to the parent when child is added is the database? Or just move one nodes to another nodes as child? Lastly thanks for your help. That was very good. This is the first time I using tree view, I take me half and hour to think what is the way to delete the nodes of tree view. :)
 
Hi again!

Yes it is. In several our applications is realized its. It's used comparatively difficult recursive function for creating of tree. Also table structure is developed especially for them (2 related fields in same table - unique field and field for entering of belongs). Of course it's possibly to solve it with related tables. This second way I'll describe as example from my application.

1. Tables' structure

XUNT - Unit labels
TypeUnitNo Long Primary
TypeUnitVersNo Integer Primary

TypeUnitName Text 50

XUNT - Sub Unit structure
TypeUnitNo Long Primary
TypeUnitVersNo Integer Primary
TypeUnitStrNo Integer Primary (Sequence number)
TypeSubUnitNo Long Primary
TypeSubUnitVersNo Integer Primary

SubUnitAmount Integer

Relations fields (ono to many) between tables are marked red. In such case are used fields for unit versions (complex structure, but it's possibly to simplify it - create tables without VersNo fields (font Italic))

2. Function for creating of tree:

Function UnitTree(blnTop As Boolean, trvTypeUnit As Object, strParent As String, strParentVer As String, strTypeUnitNo As String, strVer As String)
On Error GoTo Err_UnitTree
Dim strSQL As String
Dim strFrom As String
Dim strWhere As String

Dim NodX As Node
Dim strParentKey As String
Dim strKey As String
Dim i As Byte
Dim btyAmount As Byte
Dim rst As Recordset
Dim errCount As Byte

TryAgain:
strKey = "TU0000"
If blnTop = True Then
trvTypeUnit.Nodes.Clear
strSQL = "SELECT XUNT.TypeUnitNo, XUNT.TypeUnitVersNo, XUNT.TypeUnitName "
strFrom = "FROM XUNT "
strWhere = "WHERE XUNT.TypeUnitNo=" & strTypeUnitNo & " AND XUNT.TypeUnitVersNo=" & strVer
strSQL = strSQL & strFrom & strWhere & ";"
Set rst = CurrentDb.OpenRecordset(strSQL)
Set NodX = trvTypeUnit.Nodes.Add(, , strKey, CStr(rst(2)), "CLOSED", "SELECTED")
NodX.ExpandedImage = "OPEN"
End If

strParentKey = strKey

strSQL = "SELECT XUNT.TypeUnitNo, XUNT.TypeUnitVersNo, XUNT.TypeUnitName, XSTR.TypeUnitStrNo, XSTR.TypeSubUnitNo, XSTR.TypeSubUnitVersNo, XSTR.SubUnitAmount, XUNT_1.TypeUnitName "
strFrom = "FROM (XUNT LEFT JOIN XSTR ON (XUNT.TypeUnitVersNo = XSTR.TypeUnitVersNo) AND (XUNT.TypeUnitNo = XSTR.TypeUnitNo)) LEFT JOIN XUNT AS XUNT_1 ON (XSTR.TypeSubUnitVersNo = XUNT_1.TypeUnitVersNo) AND (XSTR.TypeSubUnitNo = XUNT_1.TypeUnitNo) "
strWhere = "WHERE XSTR.TypeSubUnitNo Is Not Null And XUNT.TypeUnitNo=" & strTypeUnitNo & " AND XUNT.TypeUnitVersNo=" & strVer
strSQL = strSQL & strFrom & strWhere & ";"
Set rst = CurrentDb.OpenRecordset(strSQL)
If Not rst.EOF Then
Do While Not rst.EOF
For i = 1 To rst(6)
strKey = "TU" & Format(CStr(trvTypeUnit.Nodes.Count + 1), "0000")
Set NodX = trvTypeUnit.Nodes.Add(strParentKey, 4, strKey, CStr(rst(7)), "CLOSED", "SELECTED")
NodX.ExpandedImage = "OPEN"
Call UnitTreeSub(False, trvTypeUnit, strKey, CStr(rst(4)), CStr(rst(5)))
Next i
rst.MoveNext
Loop
End If
rst.Close
Set rst = Nothing
trvTypeUnit.Nodes(1).Expanded = True
trvTypeUnit.Visible = True

Exit_UnitTree:
Exit Function
Err_UnitTree:
If Err.Number = -2147417848 Then
If errCount < 3 Then
GoTo TryAgain
End If
End If
End Function

Function UnitTreeSub(blnTop As Boolean, trvTypeUnit As Object, strParent As String, strTypeUnitNo As String, strVer As String)
Dim strSQL As String
Dim strFrom As String
Dim strWhere As String

Dim NodX As Node
Dim strParentKey As String
Dim strKey As String
Dim i As Byte
Dim btyAmount As Byte
Dim rst As Recordset


If blnTop = False Then

strSQL = &quot;SELECT XUNT.TypeUnitNo, XUNT.TypeUnitVersNo, XUNT.TypeUnitName, XSTR.TypeUnitStrNo, XSTR.TypeSubUnitNo, XSTR.TypeSubUnitVersNo, XSTR.SubUnitAmount, XUNT_1.TypeUnitName &quot;
strFrom = &quot;FROM (XUNT LEFT JOIN XSTR ON (XUNT.TypeUnitVersNo = XSTR.TypeUnitVersNo) AND (XUNT.TypeUnitNo = XSTR.TypeUnitNo)) LEFT JOIN XUNT AS XUNT_1 ON (XSTR.TypeSubUnitVersNo = XUNT_1.TypeUnitVersNo) AND (XSTR.TypeSubUnitNo = XUNT_1.TypeUnitNo) &quot;
strWhere = &quot;WHERE XSTR.TypeSubUnitNo Is Not Null And XUNT.TypeUnitNo=&quot; & strTypeUnitNo & &quot; AND XUNT.TypeUnitVersNo=&quot; & strVer
strSQL = strSQL & strFrom & strWhere & &quot;;&quot;
Set rst = CurrentDb.OpenRecordset(strSQL)
If Not rst.EOF Then
Do While Not rst.EOF
For i = 1 To rst(6)
strKey = &quot;TU&quot; & Format(CStr(trvTypeUnit.Nodes.Count + 1), &quot;0000&quot;)
Set NodX = trvTypeUnit.Nodes.Add(strParent, 4, strKey, CStr(rst(7)), &quot;CLOSED&quot;, &quot;SELECTED&quot;)
NodX.ExpandedImage = &quot;OPEN&quot;
Call UnitTreeSub(False, trvTypeUnit, strKey, CStr(rst(4)), CStr(rst(5)))
Next i
rst.MoveNext
Loop
End If
rst.Close
Set rst = Nothing
End If
End Function

I'm sorry but now I have not so much time for detailed description. If you'll not make out how to run functions send me e-mail later.

Good luck!
Aivars

alaganovskis@hotmail.com
 
Hi Aivars,
Wah..ur code is too deep for me. I think i need some time to digest it..:). Now i facing new problem, actually is quite simple one. I have my form 1 that have tree view. and form 2 as find item form. I created my nodes according to itemSID. So by trigger out my itemSID, I will be able to point the specific node. My question is how i going to select the nodes and make this function , treeview1_nodeclick(byval node as node), execute and display the node details on left hand side. Thanks.
 
Hi again!

Here is example:

private sub treeview1_nodeclick(byval node as node)
dim frm as form
dim strSQL as string

set frm=forms(&quot;form2&quot;)


'Node contain information what you can reclaim for your needs:
'node.key - unique for each node (text)
'node.text - label of node (text)
'node.index - unique for each node (long)
'and more other
'See at Immediate window this information


debug.print node.key, node.text, node.index

'If node.key is same itemSID
'on your opened form 2 then you can e.g.
'set recordsource of this form or
'take some other operation


strSQL=&quot;Select * From MyTable Where itemSID='&quot; & _
node.key & &quot;';&quot;
frm.recordsource=strsql
end sub


For correct work of your application you may write codes like its in the procedure ...Collapse.

Good luck!
Aivars
 
Hi,
OK, thanks for ur idea. I will try on it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top