IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VBA Access Discussion :

Un treeview qui ne veut pas être trier [AC-2016]


Sujet :

VBA Access

  1. #1
    Membre actif

    Profil pro
    Inscrit en
    Juin 2002
    Messages
    291
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 291
    Points : 270
    Points
    270
    Par défaut Un treeview qui ne veut pas être trier
    Bonjour,

    Je tente de trier un treeview. J'utilise sorted=true, mais le noeud est ajouté à la suite des autres.

    Voici le code qui rempli le treeview, qui fonctionne très bien.
    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
    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
     
    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
        ocxTreeView.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
    Maintenant le code qui me permet d'insérer un noeud. Pour tester, j'ai inséré dans le formulaire, 3 zones de texte txtNumN0, txtNumN1 et txtLabel
    Il fonctionne mais le noeud est ajouté sans trie.
    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
     
    Private Sub btAjoNoeud_Click()
     
        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
     
     
        Dim objNode As Node
     
        If IsNull(Me.txtNumN1) Then
     
            ' Ajouter un noeud Niveau 0
            Set objNode = ocxTree.Nodes.Add(, , "Niv0-" & Me.txtNumN0, Me.txtLabel & " - " & Me.txtNumN0 & " / " & Me.txtNumN1, "Image2")
            objNode.Sorted = True
            objNode.Tag = "Niveau 0"
     
        Else
     
            ' Ajouter un noeud enfant
            Set objNode = ocxTree.Nodes.Add("Niv0-" & Me.txtNumN0, tvwChild, "Niv1-" & Me.txtNumN1, Me.txtLabel & " - " & Me.txtNumN0 & " / " & Me.txtNumN1, "Image3")
            objNode.Sorted = True
            objNode.Tag = "Niveau 1"
     
        End If
     
    End Sub
    Merci d'avance pour le coup de main!

    Gdal

  2. #2
    Membre actif

    Profil pro
    Inscrit en
    Juin 2002
    Messages
    291
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 291
    Points : 270
    Points
    270
    Par défaut
    Il fallait ajouter ocxTree.Sorted = True

    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
    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
    Gdal

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Cookies] Cookie qui ne veut pas être changer
    Par Dark Cid dans le forum Langage
    Réponses: 8
    Dernier message: 15/04/2009, 17h13
  2. [JScrollPane] qui ne veut pas se mettre en haut a gauche
    Par Cyber@l dans le forum AWT/Swing
    Réponses: 4
    Dernier message: 24/11/2006, 10h41
  3. requetes qui ne veut pas passer
    Par suya95 dans le forum Requêtes
    Réponses: 14
    Dernier message: 04/07/2006, 14h17
  4. JOptionPane qui ne veut pas se fermer!
    Par benthebest dans le forum AWT/Swing
    Réponses: 6
    Dernier message: 29/12/2005, 22h05
  5. un fichier qui ne veut pas être supprimé!!!!
    Par en_stage dans le forum Autres Logiciels
    Réponses: 4
    Dernier message: 22/10/2005, 01h08

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo