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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
Private Sub Form_Open(Cancel As Integer)
DoCmd.Maximize
Call ChargerocxTree(Me.ocxTree.Object)
End Sub
Public Sub ChargerocxTree(ocxTreeView As MSComctlLib.TreeView)
Dim ocxTree As MSComctlLib.TreeView
Dim objNode As Node
Dim acxImageList As New MSComctlLib.ImageList
With acxImageList
' Supprimer toutes les images de la liste
.ListImages.Clear
' Définir la dimension des images
.ImageHeight = 48 'Hauteur
.ImageWidth = 48 'Largeur
' Charger les nouvelles images
.ListImages.Add , "Image1", LoadPicture(Application.CurrentProject.Path & "\icônes\1.jpg")
.ListImages.Add , "Image2", LoadPicture(Application.CurrentProject.Path & "\icônes\2.jpg")
.ListImages.Add , "Image3", LoadPicture(Application.CurrentProject.Path & "\icônes\3.jpg")
.ListImages.Add , "Image4", LoadPicture(Application.CurrentProject.Path & "\icônes\4.jpg")
End With
' Intialiser le TreeView
Set ocxTree = ocxTreeView
ocxTree.Nodes.Clear
ocxTree.Sorted = True
ocxTree.ImageList = acxImageList
' Charger TreeView
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
Dim strSQL As String
strSQL = "SELECT T_Mob_Mobil.ID AS TMob_ID, T_Mob_Mobil.Repere AS TMob_Repere, T_Mob_Mobil.Designation AS TMob_Designation, " & _
"T_Mob_Eqpt.ID AS TMobEqpt_ID, T_Mob_Eqpt.Repere AS TMobEqpt_Repere, T_Mob_Eqpt.Designation AS TMobEqpt_Designation " & _
"FROM T_Mob_Mobil " & _
"LEFT JOIN T_Mob_Eqpt ON T_Mob_Mobil.ID = T_Mob_Eqpt.ID_T_Mob_Mobil " & _
"ORDER BY T_Mob_Mobil.Repere, T_Mob_Eqpt.Repere;"
Dim strKeyNiv0 As String
Dim strKeyNiv1 As String
Dim strLabel As String
Dim strIDPrec As String
With rst
.Open strSQL, CurrentProject.Connection, adOpenForwardOnly
.MoveFirst
While Not (.EOF)
' Niveau 0 du TreeView
If VBA.StrComp(strIDPrec, rst!TMob_ID, vbTextCompare) <> 0 Then
' Créer la clé du niveau et du label
strKeyNiv0 = "Niv0-" & rst!TMob_ID
strLabel = rst!TMob_Repere & " - " & rst!TMob_Designation
' Insérer le noeud
Set objNode = ocxTree.Nodes.Add(, , strKeyNiv0, strLabel, "Image1", "Image2")
objNode.Tag = "Niveau 0"
objNode.Bold = True
End If
' Niveau 1 du TreeView
If rst!TMobEqpt_ID <> "" Then
' Créer la clé du niveau et du label
strKeyNiv1 = "Niv1-" & rst!TMobEqpt_ID
strLabel = rst!TMobEqpt_Repere & " - " & rst!TMobEqpt_Designation
' Insérer le noeud
Set objNode = ocxTree.Nodes.Add(strKeyNiv0, tvwChild, strKeyNiv1, strLabel, "Image3")
objNode.Sorted = True
objNode.Tag = "Niveau 1"
End If
' Mémoriser ID
strIDPrec = rst!TMob_ID
' Enregistrement suivant
.MoveNext
Wend
.Close
End With
Set rst = Nothing
' Parcourir le TreeView pour ajouter un label au 1er niveau
Dim intCpt As Integer
Dim strKeyItem As String
For intCpt = 1 To ocxTree.Nodes.Count
' Le nombre de séparateur "\" donne le niveau du noeud
If UBound(VBA.Split(ocxTree.Nodes.Item(intCpt).FullPath, "\")) = 0 Then
' Récupérer
strKeyItem = ocxTree.Nodes.Item(intCpt).Key
' Ajouter le noeud
Set objNode = ocxTree.Nodes.Add(strKeyItem, tvwChild, "AJONiv0-" & strKeyItem, "> Ajouter un équipement <", "Image4")
objNode.Tag = "Ajouter équipement"
objNode.ForeColor = VBA.RGB(0, 0, 255)
End If
Next intCpt
' Sélectionner le 1er noeud
If ocxTree.Nodes.Count > 1 Then
ocxTree.Nodes.Item(1).Selected = True
End If
End Sub |
Partager