473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Tree view adding duplcate keys

Hi Guys,

I need some help with a problem related to the looping through a
recordset to display on a tree view control.

My table has no Primary key - because duplicates are allowed.

ID parent
1
2 1
3 1
4 1
5
1 5

I would like to display this in a treeview like such

1
- 2
- 3
- 4
5
- 1
- 2
- 3
- 4

The code I am using below throws error 35602 as soon as it tries to
start list the 1 series of numbers.

Does anyone know of a way to code around this please??

Private Sub Form_Load()
Const strTableQueryName = "query2"
Dim db As DAO.Database, rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset(strTableQueryName, dbOpenDynaset,
dbReadOnly)
AddBranch rst:=rst, strPointerField:="parent", strIDField:="DBaseID",
strTextField:="text2" ', varReportToID:="DBaseID"
End Sub

'================= AddBranch Sub Procedure ======================
' Recursive Procedure to add branches to TreeView Control
'Requires:
' ActiveX Control: TreeView Control
' Name: xTree
'Parameters:
' rst: Self-referencing Recordset containing the data
' strPointerField: Name of field pointing to parent's primary key
' strIDField: Name of parent's primary key field
' strTextField: Name of field containing text to be displayed
'================================================= ============
Sub AddBranch(rst As Recordset, strPointerField As String, strIDField
As String, strTextField As String, _
Optional varReportToID As Variant)
On Error GoTo ErrorHandler

Dim x
' On Error Resume Next

Dim nodCurrent As Node, objTree As TreeView
Dim strCriteria As String, strText As String, strKey As String
Dim nodParent As Node, bk As String
Set objTree = Me!xTree.Object
If IsMissing(varReportToID) Then ' Root Branch.
strCriteria = strPointerField & " Is Null"
Else ' Search for records pointing to parent.
strCriteria = BuildCriteria(strPointerField,
rst.Fields(strPointerField).Type, "=" & varReportToID)
Set nodParent = objTree.Nodes("a" & varReportToID)
End If

' Find the first emp to report to the boss node.
rst.FindFirst strCriteria
Do Until rst.NoMatch
' Create a string with LastName.
If Not IsNull(rst(strTextField)) Then
strText = rst(strTextField)
Else
strText = "No Text"
End If

strKey = "a" & rst(strIDField)
If Not IsMissing(varReportToID) Then 'add new node to the parent
Set nodCurrent = objTree.Nodes.Add(nodParent, 4, strKey,
strText)
Else ' Add new node to the root.
Set nodCurrent = objTree.Nodes.Add(, , strKey, strText)
End If
' Save your place in the recordset so we can pass by ref for
speed.
bk = rst.Bookmark
' Add employees who report to this node.
AddBranch rst, strPointerField, strIDField, strTextField,
rst(strIDField)
rst.Bookmark = bk ' Return to last place and continue search.
rst.FindNext strCriteria ' Find next employee.
Loop

ErrorHandler:
Select Case Err.Number
Case 35602
MsgBox rst(strIDField)
Resume Next
Case 0
Case Else
MsgBox Err.Number & " " & Err.Description

End Select

End Sub
Regards

Gary

Nov 13 '05 #1
4 4787
Can you add an "AutoNumber" field to your table so you have a unique key?
This then can be uses as the hidden 'key' for the treeview

strKey = "a" & rst(AutoNumberField)
strText = rst(TextField)
Set nodCurrent = objTree.Nodes.Add(nodParent, 4, strKey, strText)

You will need to reference the "parent" to the autonumber value as well

--
Regards,
Kevin
"ghanley" <gh*****@hcsfixit.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi Guys,

I need some help with a problem related to the looping through a
recordset to display on a tree view control.

My table has no Primary key - because duplicates are allowed.

ID parent
1
2 1
3 1
4 1
5
1 5

I would like to display this in a treeview like such

1
- 2
- 3
- 4
5
- 1
- 2
- 3
- 4

The code I am using below throws error 35602 as soon as it tries to
start list the 1 series of numbers.

Does anyone know of a way to code around this please??

Private Sub Form_Load()
Const strTableQueryName = "query2"
Dim db As DAO.Database, rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset(strTableQueryName, dbOpenDynaset,
dbReadOnly)
AddBranch rst:=rst, strPointerField:="parent", strIDField:="DBaseID",
strTextField:="text2" ', varReportToID:="DBaseID"
End Sub

'================= AddBranch Sub Procedure ======================
' Recursive Procedure to add branches to TreeView Control
'Requires:
' ActiveX Control: TreeView Control
' Name: xTree
'Parameters:
' rst: Self-referencing Recordset containing the data
' strPointerField: Name of field pointing to parent's primary key
' strIDField: Name of parent's primary key field
' strTextField: Name of field containing text to be displayed
'================================================= ============
Sub AddBranch(rst As Recordset, strPointerField As String, strIDField
As String, strTextField As String, _
Optional varReportToID As Variant)
On Error GoTo ErrorHandler

Dim x
' On Error Resume Next

Dim nodCurrent As Node, objTree As TreeView
Dim strCriteria As String, strText As String, strKey As String
Dim nodParent As Node, bk As String
Set objTree = Me!xTree.Object
If IsMissing(varReportToID) Then ' Root Branch.
strCriteria = strPointerField & " Is Null"
Else ' Search for records pointing to parent.
strCriteria = BuildCriteria(strPointerField,
rst.Fields(strPointerField).Type, "=" & varReportToID)
Set nodParent = objTree.Nodes("a" & varReportToID)
End If

' Find the first emp to report to the boss node.
rst.FindFirst strCriteria
Do Until rst.NoMatch
' Create a string with LastName.
If Not IsNull(rst(strTextField)) Then
strText = rst(strTextField)
Else
strText = "No Text"
End If

strKey = "a" & rst(strIDField)
If Not IsMissing(varReportToID) Then 'add new node to the parent
Set nodCurrent = objTree.Nodes.Add(nodParent, 4, strKey,
strText)
Else ' Add new node to the root.
Set nodCurrent = objTree.Nodes.Add(, , strKey, strText)
End If
' Save your place in the recordset so we can pass by ref for
speed.
bk = rst.Bookmark
' Add employees who report to this node.
AddBranch rst, strPointerField, strIDField, strTextField,
rst(strIDField)
rst.Bookmark = bk ' Return to last place and continue search.
rst.FindNext strCriteria ' Find next employee.
Loop

ErrorHandler:
Select Case Err.Number
Case 35602
MsgBox rst(strIDField)
Resume Next
Case 0
Case Else
MsgBox Err.Number & " " & Err.Description

End Select

End Sub
Regards

Gary
Nov 13 '05 #2
Kevin - thanks for your reply.

This I believe will only solve half of my problem.

In essence what I am trying to acheive is that I build an assembly - or
a tree like

1
- 2
- 3
-- 4

This tree may be a sub branch of 5 like

5
- 1
-- 2
-- 3
--- 4

What I want to acheive is by assigning the assembly or the tree 1 to
another parent assembly - it will list all the sub branches with the one
allocation.

So

6
-5

only allocated once

would actually list as

6
- 5
-- 1
--- 2
--- 3
---- 4

and in the table it would be entered like

AutoNum ID parent
1 1
2 2 1
3 3 1
4 4 3
5 5
6 1 5
7 6
8 5 6
Regards

Gary

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #3
I assume then you are dealing with a "sub-assembly" tree (or components)
that can be associated to more than one 'Parent assemblies'

To cut to the chase, Treeview wants a unique key, and I guess the autonumber
isn't going to do it for you easily. You should however be able to get a
unique string by joining fields together

where you currently have .... strKey = "a" & rst(strIDField)
try something like ... strKey = rst(autonumberfield) & "-" &
rstr(strIDField)

alternatively, you could change the prefix character every time you start a
new Parent Node.
Using a counter, start the first node with "1-", the second is "2-" etc

strKey = intCount & "-" & rstr(strIDField)
If Not IsMissing(varReportToID) Then 'add new node to the parent
Set nodCurrent = objTree.Nodes.Add(nodParent, 4, strKey, strText)
intCount = intCount + 1
else
...
--
Regards,
Kevin

"Gary Hanley" <gh*****@hcsfixit.com> wrote in message
news:SL***************@news.uswest.net...
Kevin - thanks for your reply.

This I believe will only solve half of my problem.

In essence what I am trying to acheive is that I build an assembly - or
a tree like

1
- 2
- 3
-- 4

This tree may be a sub branch of 5 like

5
- 1
-- 2
-- 3
--- 4

What I want to acheive is by assigning the assembly or the tree 1 to
another parent assembly - it will list all the sub branches with the one
allocation.

So

6
-5

only allocated once

would actually list as

6
- 5
-- 1
--- 2
--- 3
---- 4

and in the table it would be entered like

AutoNum ID parent
1 1
2 2 1
3 3 1
4 4 3
5 5
6 1 5
7 6
8 5 6
Regards

Gary

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #4
I think what you have is a diagram (a "regular set") but not a tree
structure. A node can't have children which are also ancestors, that is to
say, it can't point "backward".

"ghanley" <gh*****@hcsfixit.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi Guys,

I need some help with a problem related to the looping through a
recordset to display on a tree view control.

My table has no Primary key - because duplicates are allowed.

ID parent
1
2 1
3 1
4 1
5
1 5

I would like to display this in a treeview like such

1
- 2
- 3
- 4
5
- 1
- 2
- 3
- 4

The code I am using below throws error 35602 as soon as it tries to
start list the 1 series of numbers.

Does anyone know of a way to code around this please??

Private Sub Form_Load()
Const strTableQueryName = "query2"
Dim db As DAO.Database, rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset(strTableQueryName, dbOpenDynaset,
dbReadOnly)
AddBranch rst:=rst, strPointerField:="parent", strIDField:="DBaseID",
strTextField:="text2" ', varReportToID:="DBaseID"
End Sub

'================= AddBranch Sub Procedure ======================
' Recursive Procedure to add branches to TreeView Control
'Requires:
' ActiveX Control: TreeView Control
' Name: xTree
'Parameters:
' rst: Self-referencing Recordset containing the data
' strPointerField: Name of field pointing to parent's primary key
' strIDField: Name of parent's primary key field
' strTextField: Name of field containing text to be displayed
'================================================= ============
Sub AddBranch(rst As Recordset, strPointerField As String, strIDField
As String, strTextField As String, _
Optional varReportToID As Variant)
On Error GoTo ErrorHandler

Dim x
' On Error Resume Next

Dim nodCurrent As Node, objTree As TreeView
Dim strCriteria As String, strText As String, strKey As String
Dim nodParent As Node, bk As String
Set objTree = Me!xTree.Object
If IsMissing(varReportToID) Then ' Root Branch.
strCriteria = strPointerField & " Is Null"
Else ' Search for records pointing to parent.
strCriteria = BuildCriteria(strPointerField,
rst.Fields(strPointerField).Type, "=" & varReportToID)
Set nodParent = objTree.Nodes("a" & varReportToID)
End If

' Find the first emp to report to the boss node.
rst.FindFirst strCriteria
Do Until rst.NoMatch
' Create a string with LastName.
If Not IsNull(rst(strTextField)) Then
strText = rst(strTextField)
Else
strText = "No Text"
End If

strKey = "a" & rst(strIDField)
If Not IsMissing(varReportToID) Then 'add new node to the parent
Set nodCurrent = objTree.Nodes.Add(nodParent, 4, strKey,
strText)
Else ' Add new node to the root.
Set nodCurrent = objTree.Nodes.Add(, , strKey, strText)
End If
' Save your place in the recordset so we can pass by ref for
speed.
bk = rst.Bookmark
' Add employees who report to this node.
AddBranch rst, strPointerField, strIDField, strTextField,
rst(strIDField)
rst.Bookmark = bk ' Return to last place and continue search.
rst.FindNext strCriteria ' Find next employee.
Loop

ErrorHandler:
Select Case Err.Number
Case 35602
MsgBox rst(strIDField)
Resume Next
Case 0
Case Else
MsgBox Err.Number & " " & Err.Description

End Select

End Sub
Regards

Gary

Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: imani_technology_spam | last post by:
We need to present hierarchical data on a web page, the same way the tree view shows files in Windows Explorer. Here's the catch: that tree view needs to be bound to a SQL Server database. How...
0
by: Tree menu using XML | last post by:
I have one XML file that has nodes and sub node and each and every node has the attribute call visible if its value is true then diplay this node else don't display thid node, but this condition i...
7
by: Antoon Pardon | last post by:
Comments are welcome: http://www.pardon-sleeuwaegen.be/antoon/avltree.html
3
by: Saradhi | last post by:
Hi All, Here I am facing a performance problem with the TreeView Node renaming. I am displaying a hierarchy Data in a treeview in my Windows C# Application. My tree view represents an...
5
by: mittal.pradeep | last post by:
I have a table with 5 columns. Every column represents a level in a hierarchy. I need to populate a windows forms tree view control using this table. Also Is there a recursive way to populate a...
3
by: Brian Henry | last post by:
If i already have a tree view created, and want to add another new node to it, how would i do so? Is there a way to throught tags or anything? like i have this RootNode | +-- Child 1 +--...
0
by: Brishti09 | last post by:
I am tring to search for an existing nodes in tree view and change its forecolor. I am matching the tag value of the node. The program is able to search recursively through the tree and change the...
1
by: happy.john1234 | last post by:
Hi, i am a new bie in programming.I am currently doing a project that displays certain hierarchial set of data in tree view.I currently implemented this tree view to be displayed in xml file ...
8
by: BigZero | last post by:
Hello ppl, i have system date that is all the software installed on machine like Ms office ,other software what ever installed in the system it stored in Ms access database. when i query to DB it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.