Treeview Control

  • Thread starter Thread starter jtardy
  • Start date Start date
J

jtardy

Hi !

I am coming up against a difficulty on the control treeviwet, and I
hope you could help me :
I would like to do a treeview based on a table "tblTreeview" including
2 fields [ArticleFab], [ArticleComposant].
The recordings will be like that :
ArticleFab______ArticleComposant
1_______________11
1_______________12
1_______________13
12______________121
12______________122
2_______________22
2_______________23
22______________221
....


A [ArticleFab] also can be [ArticleComposant] or vice versa. Finally
the article level 1 ("father"?) would be the one which is in
[ArticleFab] and not in [ArticleComposant].
So the Treeview would look like that :

¤ 1
__|
__->11
__->12
_____|
_____->121
_____->122
_->13

¤ 2
__|
__->22
_____|
_____->221
__->23

My difficulty comes from the code, I don't know how I can supply the
treeview...

Thanks for your help.

But maybe the structure of the initial table is not very suitable, in
this case, I am waiting for your advises.

Thanks to everybody for your help.
 
Dim Tw As Access.control
Dim Rs AS DAO.Recordset

set Tw = Me.twTreeView
Set Rs = CurrentDb.OpenRecordset("SELECT DISTINCT ArticleFab FROM
tblTreeView", DbOpenSnapshot)
With Tw.Object
VBA.Err.Clear
While Not Rs.EOF
.Nodes.Add , , "f" & Rs.Fields(0).Value, Rs.Fields(0).Value
Rs.MoveNext
Wend
Rs.Close: Set Rs = Nothing

Set Rs = CurrentDb.OpenRecordset("SELECT ArticleFab,ArticleComposant FROM
tblTreeView", DbOpenSnapshot)
VBA.Err.Clear
While Not Rs.EOF
.Nodes.Add "f" & Rs.Fields(0).Value, 4, "c" & Rs.Fields(1).Value
Rs.MoveNext
Wend
Rs.Close: Set Rs = Nothing
end With

Pieter

Hi !

I am coming up against a difficulty on the control treeviwet, and I
hope you could help me :
I would like to do a treeview based on a table "tblTreeview" including
2 fields [ArticleFab], [ArticleComposant].
The recordings will be like that :
ArticleFab______ArticleComposant
1_______________11
1_______________12
1_______________13
12______________121
12______________122
2_______________22
2_______________23
22______________221
....


A [ArticleFab] also can be [ArticleComposant] or vice versa. Finally
the article level 1 ("father"?) would be the one which is in
[ArticleFab] and not in [ArticleComposant].
So the Treeview would look like that :

¤ 1
__|
__->11
__->12
_____|
_____->121
_____->122
_->13

¤ 2
__|
__->22
_____|
_____->221
__->23

My difficulty comes from the code, I don't know how I can supply the
treeview...

Thanks for your help.

But maybe the structure of the initial table is not very suitable, in
this case, I am waiting for your advises.

Thanks to everybody for your help.
 
Dim Tw As Access.control
Dim Rs AS DAO.Recordset

set Tw = Me.twTreeView
Set Rs = CurrentDb.OpenRecordset("SELECT DISTINCT ArticleFab FROM
tblTreeView", DbOpenSnapshot)
With Tw.Object
VBA.Err.Clear
While Not Rs.EOF
.Nodes.Add , , "f" & Rs.Fields(0).Value, Rs.Fields(0).Value
Rs.MoveNext
Wend
Rs.Close: Set Rs = Nothing

Set Rs = CurrentDb.OpenRecordset("SELECT ArticleFab,ArticleComposant FROM
tblTreeView", DbOpenSnapshot)
VBA.Err.Clear
While Not Rs.EOF
.Nodes.Add "f" & Rs.Fields(0).Value, 4, "c" & Rs.Fields(1).Value
Rs.MoveNext
Wend
Rs.Close: Set Rs = Nothing
end With

Pieter

Hi !

I am coming up against a difficulty on the control treeviwet, and I
hope you could help me :
I would like to do a treeview based on a table "tblTreeview" including
2 fields [ArticleFab], [ArticleComposant].
The recordings will be like that :
ArticleFab______ArticleComposant
1_______________11
1_______________12
1_______________13
12______________121
12______________122
2_______________22
2_______________23
22______________221
....


A [ArticleFab] also can be [ArticleComposant] or vice versa. Finally
the article level 1 ("father"?) would be the one which is in
[ArticleFab] and not in [ArticleComposant].
So the Treeview would look like that :

¤ 1
__|
__->11
__->12
_____|
_____->121
_____->122
_->13

¤ 2
__|
__->22
_____|
_____->221
__->23

My difficulty comes from the code, I don't know how I can supply the
treeview...

Thanks for your help.

But maybe the structure of the initial table is not very suitable, in
this case, I am waiting for your advises.

Thanks to everybody for your help.



--
 
Hi Pieter, everybody

Thanks you very much for your help.
But i've a problem with .twTreeView -> error of compilation, member of
method or data untraceable ?
I use Access 97, perhaps i miss some references ?

Excuse me for my average English

Bye



Pieter Wijnen a écrit :
 
Sorry i said a mistake. It was just a problem with the object's name !

But now the Treeview would look like that :
1
|___
|___
|___
2
|___
|___
12
|___
|___
22
|___

With this table:
ArticleFab ArticleComposant
1 11
1 12
1 13
12 121
12 122
2 22
2 23
22 221

It's not exactely what i hope ;-)

Thanks
 
my mistake

Dim Tw As Access.control
Dim Rs AS DAO.Recordset

set Tw = Me.twTreeView
Set Rs = CurrentDb.OpenRecordset("SELECT DISTINCT ArticleFab FROM
tblTreeView", DbOpenSnapshot)
With Tw.Object
VBA.Err.Clear
While Not Rs.EOF
.Nodes.Add , , "f" & Rs.Fields(0).Value, Rs.Fields(0).Value
Rs.MoveNext
Wend
Rs.Close: Set Rs = Nothing

Set Rs = CurrentDb.OpenRecordset("SELECT ArticleFab,ArticleComposant FROM
tblTreeView", DbOpenSnapshot)
VBA.Err.Clear
While Not Rs.EOF
.Nodes.Add "f" & Rs.Fields(0).Value, 4, "c" & Rs.Fields(1).Value,
Rs.Fields(1).Value ' Missed out the display value
Rs.MoveNext
Wend
Rs.Close: Set Rs = Nothing
end With

Pieter
 
my mistake

Dim Tw As Access.control
Dim Rs AS DAO.Recordset

set Tw = Me.twTreeView
Set Rs = CurrentDb.OpenRecordset("SELECT DISTINCT ArticleFab FROM
tblTreeView", DbOpenSnapshot)
With Tw.Object
VBA.Err.Clear
While Not Rs.EOF
.Nodes.Add , , "f" & Rs.Fields(0).Value, Rs.Fields(0).Value
Rs.MoveNext
Wend
Rs.Close: Set Rs = Nothing

Set Rs = CurrentDb.OpenRecordset("SELECT ArticleFab,ArticleComposant FROM
tblTreeView", DbOpenSnapshot)
VBA.Err.Clear
While Not Rs.EOF
.Nodes.Add "f" & Rs.Fields(0).Value, 4, "c" & Rs.Fields(1).Value,
Rs.Fields(1).Value ' Missed out the display value
Rs.MoveNext
Wend
Rs.Close: Set Rs = Nothing
end With

Pieter


Sorry i said a mistake. It was just a problem with the object's name !

But now the Treeview would look like that :
1
|___
|___
|___
2
|___
|___
12
|___
|___
22
|___

With this table:
ArticleFab ArticleComposant
1 11
1 12
1 13
12 121
12 122
2 22
2 23
22 221

It's not exactely what i hope ;-)

Thanks



--
 
ok ! with this code the result is now :
1
|___ 11
|___ 12
|___ 13
2
|___ 22
|___ 23
12
|___ 121
|___ 122
22
|___ 221

but 12 & 22 are not at the level 1, they must be under 12 & 22 like
that ;-)
1
|___ 11
|___ 12
|___ 121
|___ 122
|___ 13

2
|___ 22
|___ 221
|___ 23

so to define the level i use on the first node
"SELECT DISTINCT tblTreeview1.ArticleFab FROM tblTreeview AS
tblTreeview1 LEFT JOIN tblTreeview AS tblTreeview2 ON
tblTreeview1.ArticleFab = tblTreeview2.ArticleComposant WHERE
(((tblTreeview2.ArticleComposant) Is Null))"
and i've an error 35601 Element not found on .Nodes.Add "f" &
Rs.Fields(0).Value, 4, "c" & Rs.Fields(1).Value, Rs.Fields(1).Value on
the 2nd node

thank you very much...
 
there is another possibility of which I had not thought: one
[ArtccComposant] can be in several [ArtccFab], therefore in several
nodes!
for example if i add the recordset :
ArticleFab______ArticleComposant
1______________22

22 is in 1 & 2 so i've an error : key is not unique in collection !!!

thanks you very much...
 
Try something like

Dim Tw As Access.control
Dim Rs AS DAO.Recordset

set Tw = Me.twTreeView
Set Rs = CurrentDb.OpenRecordset("SELECT ArticleFab FROM
tblTreeView WHERE ARTICLEComposant Is Null", DbOpenSnapshot)
With Tw.Object
VBA.Err.Clear
While Not Rs.EOF
.Nodes.Add , , "f" & Rs.Fields(0).Value, Rs.Fields(0).Value
Rs.MoveNext
Wend
Rs.Close: Set Rs = Nothing

Set Rs = CurrentDb.OpenRecordset("SELECT ArticleFab,ArticleComposant FROM
tblTreeView Where ArticleComposant Is Not Null ORDER By
Len(ArticleComposant)", DbOpenSnapshot)
VBA.Err.Clear
While Not Rs.EOF
.Nodes.Add "f" & Rs.Fields(0).Value, 4, "c" & Rs.Fields(0).Value &
Rs.Fields(1).Value, Rs.Fields(1).Value ' Missed out the display value
Rs.MoveNext
Wend
Rs.Close: Set Rs = Nothing
end With

Pieter


jtardy said:
there is another possibility of which I had not thought: one
[ArtccComposant] can be in several [ArtccFab], therefore in several
nodes!
for example if i add the recordset :
ArticleFab______ArticleComposant
1______________22

22 is in 1 & 2 so i've an error : key is not unique in collection !!!

thanks you very much...



--
 
Try something like

Dim Tw As Access.control
Dim Rs AS DAO.Recordset

set Tw = Me.twTreeView
Set Rs = CurrentDb.OpenRecordset("SELECT ArticleFab FROM
tblTreeView WHERE ARTICLEComposant Is Null", DbOpenSnapshot)
With Tw.Object
VBA.Err.Clear
While Not Rs.EOF
.Nodes.Add , , "f" & Rs.Fields(0).Value, Rs.Fields(0).Value
Rs.MoveNext
Wend
Rs.Close: Set Rs = Nothing

Set Rs = CurrentDb.OpenRecordset("SELECT ArticleFab,ArticleComposant FROM
tblTreeView Where ArticleComposant Is Not Null ORDER By
Len(ArticleComposant)", DbOpenSnapshot)
VBA.Err.Clear
While Not Rs.EOF
.Nodes.Add "f" & Rs.Fields(0).Value, 4, "c" & Rs.Fields(0).Value &
Rs.Fields(1).Value, Rs.Fields(1).Value ' Missed out the display value
Rs.MoveNext
Wend
Rs.Close: Set Rs = Nothing
end With

Pieter
 
thanks pieter

do you have an idea for my last problem...

"there is another possibility of which I had not thought: one
[ArtccComposant] can be in several [ArtccFab], therefore in several
nodes!
for example if i add the recordset :
ArticleFab______ArticleComposant
1______________22

22 is in 1 & 2 so i've an error : key is not unique in collection !!!


thanks you very much... "
 
You have to create a unique key for each node. I typically concatenate the
various node values together to create the key, so that I'd have A1B22 and
A2B22 for the two nodes. That lets me easily decompose the trail if I need
to.
 
ok, so you think i've to create an other field in my table with the
concatenate of the 2 others fields and i'll have a unique key ?
and after, how i can modify the code to supply the treeview ?

thank you very much for your help
 
You have to create unique keys for each node. You can do that in a number of
ways: concatenate a letter with sequential or random numbers, concatenate
keys from multiple nodes, or whatever works for you.

Unfortunately, without knowing the actual code you're using, I can't offer
suggestions on how to modify it.
 
thanks good idea ;-)

i'm using this code :
'===============================================================
Public Sub FillTreeview(Tree As Object, Article_Fab As Long)
Dim Sql As String
Dim Rs As DAO.Recordset
Dim Art_Fab As Long

If Article_Fab = 0 Then ' premier niveau
Sql = "SELECT DISTINCT tblTreeview1.ArticleFab FROM tblTreeview AS
tblTreeview1 LEFT JOIN tblTreeview AS tblTreeview2 ON
tblTreeview1.ArticleFab = tblTreeview2.ArticleComposant WHERE
(((tblTreeview2.ArticleComposant) Is Null))"
Set Rs = CurrentDb.OpenRecordset(Sql, dbOpenSnapshot)

Do Until Rs.EOF
Tree.Nodes.Add , , "f" & Rs!ArticleFab, Rs!ArticleFab
Tree.Nodes("f" & Rs!ArticleFab).EnsureVisible

FillTreeview Tree, Rs!ArticleFab
Rs.MoveNext
Loop

Else
Sql = "select * from tblTreeview where [ArticleFab]=" & Article_Fab
& _
" order by ArticleComposant;"
Set Rs = CurrentDb.OpenRecordset(Sql, dbOpenSnapshot)

Do Until Rs.EOF
Tree.Nodes.Add "f" & Article_Fab, tvwChild, "f" &
Rs!ArticleComposant, Rs!ArticleComposant
Tree.Nodes("f" & Rs!ArticleComposant).EnsureVisible

FillTreeview Tree, Rs!ArticleComposant
Rs.MoveNext
Loop

End If

Rs.Close: Set Rs = Nothing

End Sub
'===============================================================
Private Sub Form_Open(Cancel As Integer)
Dim Tw As Access.Control
Set Tw = Me.twTreeView
FillTreeview Tw.Object, 0
End Sub
'===============================================================

The third fields with the concatenation was named [ArticleId].
The recordset look like this :
ArticleFab ArticleComposant ArticleId
1 11 1
1 12 2
1 13 3
12 121 4
12 122 5
2 22 6
2 23 7
22 221 8

Thanks
 
Since you're only adding 1 level of children, I think the only change you
need is to change

Tree.Nodes.Add "f" & Article_Fab, tvwChild, "f" & Rs!ArticleComposant,
Rs!ArticleComposant

to

Tree.Nodes.Add "f" & Article_Fab, tvwChild, "f" & Article_Fab & "f" &
Rs!ArticleComposant, Rs!ArticleComposant
 
Back
Top