Bonjour,
Mon treeview sur quantre niveaux hiérarchiques est bien alimenté
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 Private Sub Form_Open(Cancel As Integer) 'Fill TV Dim db As DAO.Database Dim rs As DAO.Recordset Dim strKey As String mstrFormArgs = "" Set db = CurrentDb Set rs = db.OpenRecordset( _ "SELECT * " & _ "FROM tbl_Switchboard " & _ "ORDER BY tbl_Switchboard.SB_Parent, tbl_Switchboard.SB_Order", dbOpenSnapshot, dbReadOnly Or dbForwardOnly) 'Not really required, but - who knows! :-) tvSB.Nodes.Clear With rs 'Loop through all switchboard-items, adding them to the treeview While Not .EOF 'Assemble a key for this node, consisting of three arguments: ' ' 1. The ID - required to find a child-node's parent ' 2. The "Object-type" - form, report, etc. ' 3. The "Object-Name" - the name of the form, report, etc. ' 4. Additional stuff (i.e. OpenArgs being passed to a form) ' strKey = !SB_ID & ";" & Nz(!SB_ObjectType) & ";" & Nz(!SB_ObjectName) & ";" & Nz(!SB_NodeTitle) ' strKey = !SB_ID & ";" & Nz(!SB_ObjectType) & ";" & Nz(!SB_ObjectName) & ";" & Nz(!SB_NodeTitle) & Nz(!SB_Parent) strKey = !SB_ID & ";" & Nz(!SB_ObjectType) & ";" & Nz(!SB_ObjectName) & ";" & Nz(!SB_NodeTitle) If !SB_Parent > 0 Then 'This node is a child-node of some other node; 'Find the parent node and add it below that node's last child tvSB.Nodes.Add getNodeIndex(!SB_Parent), tvwChild, strKey, !SB_NodeTitle Else 'There is no parent - add this node to the treeview's root tvSB.Nodes.Add , tvwLast, strKey, !SB_NodeTitle End If .MoveNext Wend End With 'Clean up Set rs = Nothing Set db = Nothing 'initialize the form / show the main form Me!Switchboard_Subform.SourceObject = mconMainForm 'Maximize the switchboard DoCmd.Maximize End Subquoique je me pose des questions sur l'utilisation de tvwlast, pourquoi pas tvwprevious pour le noeud parent de plus haut niveau.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 'Get a node's index from a key-string (being the first of 3 arguments separated by ";") Private Function getNodeIndex(ByVal strKey As String) As Integer Dim intCounter As Integer 'Assume failure (return root) getNodeIndex = 0 For intCounter = 1 To tvSB.Nodes.Count 'The key being looked for is the first argument If Split(tvSB.Nodes(intCounter).Key, ";")(0) = strKey Then 'Found the node getNodeIndex = intCounter Exit For End If Next intCounter End Function
je bute maintenant sur l'impossibilité de remonter jus'au noeud parent de plus haut niveau.
Quand je clique sur le noeud enfant de plus bas niveau j'ouvre bien la form souhaitée, mais je n'arrive à récupérer que
Node.parent.parent, Node.parent et Node
PAs moyen de récupérer Node.parent.parent.parent
C'est génant car j'ai besoin des libellés des quatre noeuds comme clé pour d'autres tables avec des données détails
je récupère une erreur
Erreur d'éxécution '91
Variable objet ou variable de bloc with non définie
voici le code qui me sert à envoyer la form détail et la manière de remonter dans l'arbre des noeuds de l'enfant au tout premier parent
A noter que j'ai plusieurs noeuds parents à la racine.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 Private Sub tvSB_NodeClick(ByVal Node As Object) 'User clicked a node Dim strSwitchboard_SubForm_ToShow As String Dim strForm_OpenArgs As String 'The default form to be loaded into the subform-area - if no other will be set within this sub strSwitchboard_SubForm_ToShow = mconMainForm 'Default: no contents for information being pulled from a form loaded into the subform mstrFormArgs = "" 'This statement should actually never evaluate to false, it's rather meant to further 'describe the usage of the Split()-function If UBound(Split(Node.Key, ";")) = 3 Then Dim strObjectType As String Dim strObjectName As String Dim strObjectAddtnl As String strObjectType = Split(Node.Key, ";")(1) 'second argument within the node's key strObjectName = Split(Node.Key, ";")(2) 'third argument within the node's key strObjectAddtnl = Split(Node.Key, ";")(3) 'strObjectAddtnl = Node.Parent & strObjectAddtnl 'strObjectAddtnl = Node.Parent.Parent & strObjectAddtnl 'strObjectAddtnl = Node.Parent.Parent.Parent & strObjectAddtnl 'strObjectAddtnl = Split(Node.Parent.Key, ";")(3) & strObjectAddtnl 'strObjectAddtnl = Split(Node.Parent.Parent.Key, ";")(3) & strObjectAddtnl On Error Resume Next Select Case strObjectType Case "Form" 'Just pass on the form's name - this will result in the switchboard's subform 'being set to this form. strSwitchboard_SubForm_ToShow = strObjectName mstrFormArgs = strObjectAddtnl Case "Form_Dialog" 'Load a form separately DoCmd.OpenForm FormName:=strObjectName, windowmode:=acDialog, OpenArgs:=strObjectAddtnl Case "Report" 'This is just a sample call to a report - you might want to distinguish 'between printing directly or showing the preview here. DoCmd.OpenReport reportname:=strObjectName ', OpenArgs:=strObjectAddtnl '<-- OpenArgs are possible for reports starting with Access 2002 only! Case "Code" 'For the sake of the sample, this call references a sub within the switchboard-form's 'module (hence the CodeContextObject - this could as well be <Me>). 'An alternative would be to call a function within a general module - in order to do so, 'you'll have to know that module's name (or index). The call would then look like this: ' CallByName modules(0), strObjectName, VbMethod 'or ' CallByName modules("mdl_SomeModule"), strObjectName, VbMethod CallByName CodeContextObject, strObjectName, VbMethod, strObjectAddtnl Case "" 'do nothing (user clicked a parent-node) Case Else 'Unrecognized "Ojbect-Type" - show an error-message MsgBox "Unknown Object-Type within Switchboard-table: " & strObjectType, vbExclamation, "Error" End Select End If If Me!Switchboard_Subform.SourceObject <> strSwitchboard_SubForm_ToShow Then 'If this sub resulted in a different form to be shown in the subform-area, load it Me!Switchboard_Subform.SourceObject = strSwitchboard_SubForm_ToShow End If If Err.Number <> 0 Then 'For debugging-purposes, I suggest adding a message like this. ' 'For the sample, I'm only providing one form MsgBox "This would result in a " & strObjectType & " called " & Q & strObjectName & Q & " being called," & vbCrLf & _ "but calling the object raised an exception (that object probably just doesn't exist).", _ vbInformation, "Error/bug: (Node-click)" End If If strSwitchboard_SubForm_ToShow <> mconMainForm Then 'Attempt to have a potentially loaded form refresh its information; 'In order for this to be successful, the subform will have to provide a public sub called "RefreshInfo" Err.Clear Switchboard_Subform.Form.RefreshInfo End If End Sub
Ci joint ma base de test complète zippée, (la table utilisée pour le treeview est tbl_Switchboard) si une âme charitable pouvait m'aider, je brulerai un cierge en son honneur.
Treeview_Save.zip
Partager