G
Guest
Hi All,
I am using a TreeView object to show a Bill of Materials and to output the
views path to excel for analysis.
TreeView does not allow you to use the same key twice in a veiw. My key is
created by concatenating "a" to my part key (this is because treeveiw keys
must start with an alpha.) In my case the key is a part and I will likely use
the same screw for example at several levels of the BOM.
I found a way around this problem. In the error trapping I stop error #
35602 and change the first letter to the next letter and send it through
again until it is unique.
Here is my problem. When the key changes from "a" to "b" (in the nodParent)
for a part that has children, It crashes. It does so because when building
the children, it is trying to attach them to a nodPartent field that starts
with "a" and gets lost. I can not figure out how to capture the nodParent
key when it changes from "a" and feed it back to nodCurrent.
What do you think?
Sub AddBranch(rs As Recordset, strPointerField As String, _
strIDField As String, strTextField As String, strDescField
As String, strQty As String, _
Optional varParentBranch As Variant)
On Error GoTo error_out
Dim nodCurrent As Node, objTree As TreeView
Dim strCriteria As String, strText As String, strDesc As String, strQ As
String, strKey As String, strOH As String
Dim nodParent As Node, bk As String
Dim i As Integer
Set objTree = Me!xTree.Object
If IsMissing(varParentBranch) Then
strCriteria = strPointerField & " Is Null"
Else
strCriteria = BuildCriteria(strPointerField, _
rs.Fields(strPointerField).Type, "=" & varParentBranch)
Set nodParent = objTree.Nodes("a" & varParentBranch)
End If
rs.FindFirst strCriteria
Do Until rs.NoMatch
strText = rs(strTextField)
strDesc = rs(strDescField)
strQ = rs(strQty)
strKey = "a" & rs(strIDField)
chgDupKey:
If Not IsMissing(varParentBranch) Then
Set nodCurrent = objTree.Nodes.Add(nodParent, tvwChild, strKey,
strText & "|" & strDesc & "|" & strQ)
nodCurrent.Expanded = True
Else
Set nodCurrent = objTree.Nodes.Add(, , strKey, strText & "|" &
strDesc & "|" & strQ)
nodCurrent.Expanded = True
End If
bk = rs.Bookmark
AddBranch rs, strPointerField, strIDField, strTextField, strDescField,
strQty, rs(strIDField)
rs.Bookmark = bk
rs.FindNext strCriteria
Loop
exit_out:
Exit Sub
error_out:
If err.Number = 35602 Then ' This is the error for duplicate key present
MsgBox "Duplicate Part, attempting to change key: " & strKey & _
Chr(13) + Chr(10) & nodCurrent.FullPath, vbOKOnly, "Duplicate
Part"
strKey = Chr(Asc(Left(strKey, 1)) + 1) & rs(strIDField)
Resume chgDupKey
Else
MsgBox "Error Number " & err.Number & Chr(13) + Chr$(10) &
err.Description
End If
Resume exit_out
End Sub
I am using a TreeView object to show a Bill of Materials and to output the
views path to excel for analysis.
TreeView does not allow you to use the same key twice in a veiw. My key is
created by concatenating "a" to my part key (this is because treeveiw keys
must start with an alpha.) In my case the key is a part and I will likely use
the same screw for example at several levels of the BOM.
I found a way around this problem. In the error trapping I stop error #
35602 and change the first letter to the next letter and send it through
again until it is unique.
Here is my problem. When the key changes from "a" to "b" (in the nodParent)
for a part that has children, It crashes. It does so because when building
the children, it is trying to attach them to a nodPartent field that starts
with "a" and gets lost. I can not figure out how to capture the nodParent
key when it changes from "a" and feed it back to nodCurrent.
What do you think?
Sub AddBranch(rs As Recordset, strPointerField As String, _
strIDField As String, strTextField As String, strDescField
As String, strQty As String, _
Optional varParentBranch As Variant)
On Error GoTo error_out
Dim nodCurrent As Node, objTree As TreeView
Dim strCriteria As String, strText As String, strDesc As String, strQ As
String, strKey As String, strOH As String
Dim nodParent As Node, bk As String
Dim i As Integer
Set objTree = Me!xTree.Object
If IsMissing(varParentBranch) Then
strCriteria = strPointerField & " Is Null"
Else
strCriteria = BuildCriteria(strPointerField, _
rs.Fields(strPointerField).Type, "=" & varParentBranch)
Set nodParent = objTree.Nodes("a" & varParentBranch)
End If
rs.FindFirst strCriteria
Do Until rs.NoMatch
strText = rs(strTextField)
strDesc = rs(strDescField)
strQ = rs(strQty)
strKey = "a" & rs(strIDField)
chgDupKey:
If Not IsMissing(varParentBranch) Then
Set nodCurrent = objTree.Nodes.Add(nodParent, tvwChild, strKey,
strText & "|" & strDesc & "|" & strQ)
nodCurrent.Expanded = True
Else
Set nodCurrent = objTree.Nodes.Add(, , strKey, strText & "|" &
strDesc & "|" & strQ)
nodCurrent.Expanded = True
End If
bk = rs.Bookmark
AddBranch rs, strPointerField, strIDField, strTextField, strDescField,
strQty, rs(strIDField)
rs.Bookmark = bk
rs.FindNext strCriteria
Loop
exit_out:
Exit Sub
error_out:
If err.Number = 35602 Then ' This is the error for duplicate key present
MsgBox "Duplicate Part, attempting to change key: " & strKey & _
Chr(13) + Chr(10) & nodCurrent.FullPath, vbOKOnly, "Duplicate
Part"
strKey = Chr(Asc(Left(strKey, 1)) + 1) & rs(strIDField)
Resume chgDupKey
Else
MsgBox "Error Number " & err.Number & Chr(13) + Chr$(10) &
err.Description
End If
Resume exit_out
End Sub