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.
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 = "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(strParent, 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
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.
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.
private sub treeview1_nodeclick(byval node as node)
dim frm as form
dim strSQL as string
set frm=forms("form2"
'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="Select * From MyTable Where itemSID='" & _
node.key & "';"
frm.recordsource=strsql
end sub
For correct work of your application you may write codes like its in the procedure ...Collapse.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.