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 :

Filtre multicritère sur formulaire - ACCESS 2007 [AC-2007]


Sujet :

VBA Access

  1. #1
    Candidat au Club
    Homme Profil pro
    responsable audiovisuel
    Inscrit en
    Octobre 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : responsable audiovisuel
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Octobre 2016
    Messages : 4
    Points : 2
    Points
    2
    Par défaut Filtre multicritère sur formulaire - ACCESS 2007
    Bonjour,

    J'ai une base Access partagée par une vingtaine de personnes.

    Dans cette base, j'ai un filtre multicritère dans l'en-tête de mon formulaire qui fonctionne à l'aide d'un bouton.
    j'ai placé le code suivant Sur l'événement "sur clic" :


    Private Sub bouton_filtre_Click()


    'filtres sur les champs du formulaire

    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
    F = ""
    If Not IsNull(Me.Rmarque) And Me.Rmarque <> "" Then
    If F <> "" Then
    F = F & " AND nom_marque LIKE ""*" & Me.Rmarque & "*"""
    Else
    F = "nom_marque LIKE ""*" & Me.Rmarque & "*"""
    End If
    End If
     
    If Not IsNull(Me.Rproduit) And Me.Rproduit <> "" Then
    If F <> "" Then
    F = F & " AND nom_produit LIKE ""*" & Me.Rproduit & "*"""
    Else
    F = "nom_produit LIKE ""*" & Me.Rproduit & "*"""
    End If
    End If
     
    If Not IsNull(Me.Rnature_produit) And Me.Rnature_produit <> "" Then
    If F <> "" Then
    F = F & " AND nom_nature_produit LIKE ""*" & Me.Rnature_produit & "*"""
    Else
    F = "nom_nature_produit LIKE ""*" & Me.Rnature_produit & "*"""
    End If
    End If
    If Not IsNull(Me.Rannonceur) And Me.Rannonceur <> "" Then
    If F <> "" Then
    F = F & " AND nom_annonceur LIKE ""*" & Me.Rannonceur & "*"""
    Else
    F = "nom_annonceur LIKE ""*" & Me.Rannonceur & "*"""
    End If
    End If
    If Not IsNull(Me.Rtitre_film) And Me.Rtitre_film <> "" Then
    If F <> "" Then
    F = F & " AND titre_film LIKE ""*" & Me.Rtitre_film & "*"""
    Else
    F = "titre_film LIKE ""*" & Me.Rtitre_film & "*"""
    End If
    End If
    If Not IsNull(Me.Rduree) And Me.Rduree <> "" Then
    If F <> "" Then
    F = F & " AND duree_film LIKE ""*" & Me.Rduree & "*"""
    Else
    F = "duree_film LIKE ""*" & Me.Rduree & "*"""
    End If
    End If
     
    If Not IsNull(Me.Rcode_arpp) And Me.Rcode_arpp <> "" Then
    If F <> "" Then
    F = F & " AND code_arpp LIKE ""*" & Me.Rcode_arpp & "*"""
    Else
    F = "code_arpp LIKE ""*" & Me.Rcode_arpp & "*"""
    End If
    End If
     
    If Not IsNull(Me.Ride12_film) And Me.Ride12_film <> "" Then
    If F <> "" Then
    F = F & " AND ide12_film LIKE ""*" & Me.Ride12_film & "*"""
    Else
    F = "ide12_film LIKE ""*" & Me.Ride12_film & "*"""
    End If
    End If
    If Not IsNull(Me.rtitre_oeuvre) And Me.rtitre_oeuvre <> "" Then
    If F <> "" Then
    F = F & " AND titre_oeuvre LIKE ""*" & Me.rtitre_oeuvre & "*"""
    Else
    F = "titre_oeuvre LIKE ""*" & Me.rtitre_oeuvre & "*"""
    End If
    End If
    If Not IsNull(Me.Rayant_droit) And Me.Rayant_droit <> "" Then
    If F <> "" Then
    F = F & " AND nom_ayant_droit LIKE ""*" & Me.Rayant_droit & "*"""
    Else
    F = "nom_ayant_droit LIKE ""*" & Me.Rayant_droit & "*"""
    End If
    End If
    If Not IsNull(Me.Rsignature) And Me.Rsignature <> "" Then
    If F <> "" Then
    F = F & " AND signature LIKE ""*" & Me.Rsignature & "*"""
    Else
    F = "signature LIKE ""*" & Me.Rsignature & "*"""
    End If
    End If
    If Not IsNull(Me.Rhistorique) And Me.Rhistorique <> "" Then
    If F <> "" Then
    F = F & " AND DernierDechoix_historique = """ & Me.Rhistorique & """"
    Else
    F = "DernierDechoix_historique like """ & Me.Rhistorique & """"
    End If
    End If
     
    If Not IsNull(Me.Rcocv) And Me.Rcocv <> "" Then
    If F <> "" Then
    F = F & " AND ide_12_oeuvre LIKE ""*" & Me.Rcocv & "*"""
    Else
    F = "ide_12_oeuvre LIKE ""*" & Me.Rcocv & "*"""
    End If
    End If
     
    Me.Filter = F
    Me.FilterOn = True
     
    End Sub
    Ce code fontionne bien même si je reconnais que ce n'est peut-être pas la meilleure syntaxe possible.

    Je souhaiterai maintenant remplacer les champs de recherche Rannonceur et Rmarque par un seul champ de recherche (que je nommerai "Rannonceur_mrq" par exemple)
    et qui rechercherait a la fois sur la colonne nom_annonceur et nom_marque, tout en fonctionnant avec le reste du filtre.

    De sorte que si j'ai 2 critères de recherche, un sur Rannonceur_mrq et un sur Rproduit (par exemple "mc picsou" et "little mac")
    le filtre me renvoie tous les enregistrements dont le nom_annonceur ou le nom_marque contient "mc picsou" ET dont le produit contient "little mac"

    j'ai testé ce code :


    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
    F = ""
     
    If Not IsNull(Me.Rannonceur_mrq) And Me.Rannonceur_mrq <> "" Then
    If F <> "" Then
     
    F = F & "nom_annonceur LIKE ""*" & Me.Rannonceur_mrq & "*""" & "or nom_marque Like ""*" & Me.Rannonceur_mrq & "*"""
     
    Else
     
    F = "nom_annonceur LIKE ""*" & Me.Rannonceur_mrq & "*""" & "or nom_marque Like ""*" & Me.Rannonceur_mrq & "*"""
     
    If Not IsNull(Me.Rproduit) And Me.Rproduit <> "" Then
    If F <> "" Then
    F = F & " AND nom_produit LIKE ""*" & Me.Rproduit & "*"""
    Else
    F = "nom_produit LIKE ""*" & Me.Rproduit & "*"""
    End If
    End If
     
    If Not IsNull(Me.Rnature_produit) And Me.Rnature_produit <> "" Then
    If F <> "" Then
    F = F & " AND nom_nature_produit LIKE ""*" & Me.Rnature_produit & "*"""
    Else
    F = "nom_nature_produit LIKE ""*" & Me.Rnature_produit & "*"""
    End If
    End If
    If Not IsNull(Me.Rannonceur) And Me.Rannonceur <> "" Then
    If F <> "" Then
    F = F & " AND nom_annonceur LIKE ""*" & Me.Rannonceur & "*"""
    Else
    F = "nom_annonceur LIKE ""*" & Me.Rannonceur & "*"""
    End If
    End If
    If Not IsNull(Me.Rtitre_film) And Me.Rtitre_film <> "" Then
    If F <> "" Then
    F = F & " AND titre_film LIKE ""*" & Me.Rtitre_film & "*"""
    Else
    F = "titre_film LIKE ""*" & Me.Rtitre_film & "*"""
    End If
    End If
    If Not IsNull(Me.Rduree) And Me.Rduree <> "" Then
    If F <> "" Then
    F = F & " AND duree_film LIKE ""*" & Me.Rduree & "*"""
    Else
    F = "duree_film LIKE ""*" & Me.Rduree & "*"""
    End If
    End If
     
    If Not IsNull(Me.Rcode_arpp) And Me.Rcode_arpp <> "" Then
    If F <> "" Then
    F = F & " AND code_arpp LIKE ""*" & Me.Rcode_arpp & "*"""
    Else
    F = "code_arpp LIKE ""*" & Me.Rcode_arpp & "*"""
    End If
    End If
     
    If Not IsNull(Me.Ride12_film) And Me.Ride12_film <> "" Then
    If F <> "" Then
    F = F & " AND ide12_film LIKE ""*" & Me.Ride12_film & "*"""
    Else
    F = "ide12_film LIKE ""*" & Me.Ride12_film & "*"""
    End If
    End If
    If Not IsNull(Me.rtitre_oeuvre) And Me.rtitre_oeuvre <> "" Then
    If F <> "" Then
    F = F & " AND titre_oeuvre LIKE ""*" & Me.rtitre_oeuvre & "*"""
    Else
    F = "titre_oeuvre LIKE ""*" & Me.rtitre_oeuvre & "*"""
    End If
    End If
    If Not IsNull(Me.Rayant_droit) And Me.Rayant_droit <> "" Then
    If F <> "" Then
    F = F & " AND nom_ayant_droit LIKE ""*" & Me.Rayant_droit & "*"""
    Else
    F = "nom_ayant_droit LIKE ""*" & Me.Rayant_droit & "*"""
    End If
    End If
    If Not IsNull(Me.Rsignature) And Me.Rsignature <> "" Then
    If F <> "" Then
    F = F & " AND signature LIKE ""*" & Me.Rsignature & "*"""
    Else
    F = "signature LIKE ""*" & Me.Rsignature & "*"""
    End If
    End If
    If Not IsNull(Me.Rhistorique) And Me.Rhistorique <> "" Then
    If F <> "" Then
    F = F & " AND DernierDechoix_historique = """ & Me.Rhistorique & """"
    Else
    F = "DernierDechoix_historique like """ & Me.Rhistorique & """"
    End If
    End If
     
    If Not IsNull(Me.Rcocv) And Me.Rcocv <> "" Then
    If F <> "" Then
    F = F & " AND ide_12_oeuvre LIKE ""*" & Me.Rcocv & "*"""
    Else
    F = "ide_12_oeuvre LIKE ""*" & Me.Rcocv & "*"""
    End If
    End If
     
    Me.Filter = F
    Me.FilterOn = True

    avec le 1er critère il fonctionne.
    En revanche quand je rajoute le 2ème critère, il me renvoie bien tous les enregistrements dont le champ "nom_annonceur" ou le champ "nom_marque" contiennent "mc picsou" mais parmi ceux là, il me renvoie également ceux qui ne contiennent pas le 2ème critère "little mac".

    Je suppose que c'est un problème de logique. Quelqu'un aurait une idée s'il vous plait?

    D'avance merci pour votre aide,

    Thierry

    P.s Je précise que je ne suis pas développeur de métier et plutôt néophyte.

  2. #2
    Expert confirmé Avatar de nico84
    Homme Profil pro
    Consultant/développeur ERP
    Inscrit en
    Mai 2008
    Messages
    3 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Consultant/développeur ERP
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 3 087
    Points : 5 203
    Points
    5 203
    Par défaut
    Bonjour,

    Il manque un espace devant or et surtout il faut des parenthèses sur le 1er bloc pour respecter les règles souhaitées :
    Utilisez Planet, gestion d'entreprise gratuite pour TPE / PME

  3. #3
    Candidat au Club
    Homme Profil pro
    responsable audiovisuel
    Inscrit en
    Octobre 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : responsable audiovisuel
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Octobre 2016
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Merci, je viens d'essayer mais ça ne change rien, à moins que les parenthèses ne soient pas au bon endroit ?


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    F = ""
     
    If Not IsNull(Me.Rannonceur_mrq) And Me.Rannonceur_mrq <> "" Then
    If F <> "" Then
     
    F = F & ("nom_annonceur LIKE ""*" & Me.Rannonceur_mrq & "*""" & " or nom_marque Like ""*" & Me.Rannonceur_mrq & "*""")
     
    Else
     
    F = ("nom_annonceur LIKE ""*" & Me.Rannonceur_mrq & "*""" & " or nom_marque Like ""*" & Me.Rannonceur_mrq & "*""")
     
    End If
    End If

  4. #4
    Expert confirmé Avatar de nico84
    Homme Profil pro
    Consultant/développeur ERP
    Inscrit en
    Mai 2008
    Messages
    3 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Consultant/développeur ERP
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 3 087
    Points : 5 203
    Points
    5 203
    Par défaut
    Ah ben non, les parenthèses doivent être DANS la string
    > msgbox F
    Utilisez Planet, gestion d'entreprise gratuite pour TPE / PME

  5. #5
    Candidat au Club
    Homme Profil pro
    responsable audiovisuel
    Inscrit en
    Octobre 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : responsable audiovisuel
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Octobre 2016
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Ah oui lol merci beaucoup ! Ca fonctionne nickel !!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    F = ""
     
    If Not IsNull(Me.Rannonceur_mrq) And Me.Rannonceur_mrq <> "" Then
    If F <> "" Then
     
    F = F & "(nom_annonceur LIKE ""*" & Me.Rannonceur_mrq & "*""" & " or nom_marque Like ""*" & Me.Rannonceur_mrq & "*"")"
     
    Else
     
    F = "(nom_annonceur LIKE ""*" & Me.Rannonceur_mrq & "*""" & " or nom_marque Like ""*" & Me.Rannonceur_mrq & "*"")"
     
    End If
    End If

  6. #6
    Expert confirmé Avatar de nico84
    Homme Profil pro
    Consultant/développeur ERP
    Inscrit en
    Mai 2008
    Messages
    3 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Consultant/développeur ERP
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 3 087
    Points : 5 203
    Points
    5 203
    Par défaut
    Et sinon, c'est plus lisible en utilisant ' qui d'ailleurs est le standard SQL
    Utilisez Planet, gestion d'entreprise gratuite pour TPE / PME

  7. #7
    Candidat au Club
    Homme Profil pro
    responsable audiovisuel
    Inscrit en
    Octobre 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : responsable audiovisuel
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Octobre 2016
    Messages : 4
    Points : 2
    Points
    2
    Par défaut
    Ok, merci pour l'info !

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

Discussions similaires

  1. Ouvrir un formulaire Access 2007 sur un record spécifique
    Par houleyh dans le forum VBA Access
    Réponses: 9
    Dernier message: 28/09/2013, 12h08
  2. Filtres sur formulaires Access
    Par Erin78 dans le forum IHM
    Réponses: 3
    Dernier message: 14/09/2012, 08h59
  3. Réponses: 0
    Dernier message: 21/04/2008, 12h27
  4. PB code et filtre multicritères sur formulaire
    Par flagfight dans le forum Access
    Réponses: 3
    Dernier message: 28/04/2006, 16h36
  5. Programmation d'un filtre particulier sur formulaire
    Par student dans le forum Access
    Réponses: 2
    Dernier message: 24/02/2006, 12h34

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