[Access] Requête SQL VBA
Bonjour à tous,
Je suis en cours d'élaboration de graphique sous access et je souhaite actualiser ceux ci via des choix faits depuis le formulaire ( Bouton & liste de choix).
J'ai créer une procédure afin de générer une nouvelle chaine SQL en envoyer dans le rowsource du graphique, mais celui ci n'est pas pris en compte lors du "Load" du formulaire ou même sur l'action d'un bouton ou liste de choix.
Private Sub Form_Load()
Dim Mois$, i&
Dim db As Database
Dim rs As Recordset
Dim chaineSQL As String
Set db = CurrentDb
BP1 = True
For Each c In Me.Controls
If c.Name Like "BP2_*" Then c.Visible = False Else c.Visible = True
Next c
For Each c In Me.Controls ' Activation des boutons qui correspond au mois inférieur ou égale à Mois(Now())
If c.Name Like "BP_Mois*" Then
If CInt(Split(c.Name, "_")(2)) <= CInt(Month(Now())) Then
c.Enabled = True: c.Value = True
Else
c.Enabled = False: c.Value = False ' Désactivation des boutons suéprieur à Mois(Now())
End If
End If
If c.Name = "Combo_Date" Then ' Alimentation de la liste de choix pour les Années
chaineSQL = "Select Distinct Year (Liste_Produit.Date_Interaction) from Liste_Produit;"
Set rs = db.OpenRecordset(chaineSQL)
If rs.RecordCount > 0 Then ' Si résultat de la commande SQL < 0
rs.MoveFirst ' Lecture du 1er enregistrement
Do While Not rs.EOF() ' Boucle sur tous les enregistrements
Combo_Date.AddItem rs.Fields(0)
rs.MoveNext ' Enregistrement suivant
Loop
For i = 0 To Combo_Date.ListCount - 1 ' Sélectionne l'année Now() par défaut
If CInt(Combo_Date.ItemData(i)) = CInt(Year(Now())) Then Combo_Date.Selected(i) = True: Exit For
Next i
End If
End If
If c.Name = "BP1_Combo_AS" Then ' Alimentation de la liste de choix pour les AS
chaineSQL = "Select Distinct Ucase (Liste_Produit.Nom_AS) from Liste_Produit;"
Set rs = db.OpenRecordset(chaineSQL)
If rs.RecordCount > 0 Then ' Si résultat de la commande SQL < 0
rs.MoveFirst ' Lecture du 1er enregistrement
Do While Not rs.EOF() ' Boucle sur tous les enregistrements
If Admin = False Then
If rs.Fields(0) = UCase(Nom_User) & " " & UCase(Prenom_User) Then BP1_Combo_AS.AddItem rs.Fields(0)
BP1_Combo_AS.Enabled = False
Else
BP1_Combo_AS.AddItem rs.Fields(0): BP1_Combo_AS.Enabled = True
End If
For i = 0 To BP1_Combo_AS.ListCount - 1 'AS
If BP1_Combo_AS.ItemData(i) = UCase(Nom_User) & " " & UCase(Prenom_User) Then Me.BP1_Combo_AS.Selected(i) = True: Exit For
Next i
rs.MoveNext ' Enregistrement suivant
Loop
End If
End If
If c.Name = "BP1_Combo_Produit" Then ' Alimentation de la liste de choix pour les Produits
chaineSQL = "Select Distinct Ucase (Liste_Produit.Produit) from Liste_Produit;"
Set rs = db.OpenRecordset(chaineSQL)
If rs.RecordCount > 0 Then ' Si résultat de la commande SQL < 0
rs.MoveFirst ' Lecture du 1er enregistrement
Do While Not rs.EOF() ' Boucle sur tous les enregistrements
BP1_Combo_Produit.AddItem rs.Fields(0)
rs.MoveNext ' Enregistrement suivant
Loop
End If
End If
Next c
Call MAJ_Requete
End SubJ'appelle par la suite "MaJ_Requete" a fin de pouvoir envoyer cette nouvelle requête dans le graphique, mais rien ne se passe:
Private Sub MAJ_Requete()
Dim ch_Mois$, ch_AS$, ch_Produit$, ch_Annee$
Dim chaine, chaineSQL
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
For i = 1 To 12 ' Récupére les Mois sélectionner de type 'int'
If Me.Controls("BP_Mois_" & i) = True Then
If Len(ch_Mois) = 0 Then
ch_Mois = "Month(Liste_Produit.[date_Interaction])=" & i
Else
ch_Mois = ch_Mois & " and Month(Liste_Produit.[date_Interaction])=" & i
End If
End If
Next i
For Each c In BP1_Combo_AS.ItemsSelected ' Récupére les AS sélectionner de type 'String'
If Len(ch_AS) = 0 Then
ch_AS = "Liste_Produit.[Nom_AS]='" & BP1_Combo_AS.ItemData(c) & "'"
Else
ch_AS = ch_AS & " and Liste_Produit.[Nom_AS]='" & BP1_Combo_AS.ItemData(c) & "'"
End If
Next c
For Each c In BP1_Combo_Produit.ItemsSelected ' Récupére les Produits sélectionner de type 'String'
If Len(ch_Produit) = 0 Then
ch_Produit = "Liste_Produit.[Produit]='" & BP1_Combo_Produit.ItemData(c) & "'"
Else
ch_Produit = ch_Produit & " and Liste_Produit.[Produit]='" & BP1_Combo_Produit.ItemData(c) & "'"
End If
Next c
For Each c In Combo_Date.ItemsSelected ' Récupére les Années sélectionner de type 'int'
If Len(ch_Annee) = 0 Then
ch_Annee = "Year(Liste_Produit.[date_Interaction])=" & CInt(Combo_Date.ItemData(c))
Else
ch_Annee = ch_Annee & " and Year(Liste_Produit.[date_Interaction])=" & Combo_Date.ItemData(c)
End If
Next c
liste = Array(ch_Annee, ch_AS, ch_Mois, ch_Produit)
'Création de la clause 'Where' en fonction des champs remplis
For Each c In liste
If Len(c) > 0 Then
If Len(chaine) = 0 Then
chaine = c
Else
chaine = chaine & " and " & c
End If
End If
Next c
chaineSQL = "TRANSFORM Count(Liste_Produit.[Produit]) AS CountOfProduit " & _
"SELECT Liste_Produit.[Nom_AS] " & _
"FROM Liste_Produit " & _
"WHERE (" & chaine & ")" & _
"GROUP BY Liste_Produit.[Nom_AS] " & _
"ORDER BY Liste_Produit.[Nom_AS] " & _
"PIVOT Liste_Produit.[Produit];"
'Set rs = db.OpenRecordset(chaineSQL)
Debug.Print (chaineSQL)
Me.BP1_View1_Graph1.RowSource = ""
Me.BP1_View1_Graph1.RowSource = chaineSQL
Me.BP1_View1_Graph1.Requery
End SubChaineSQL me renvois la valeur :
TRANSFORM Count(Liste_Produit.[Produit]) AS CountOfProduit SELECT Liste_Produit.[Nom_AS] FROM Liste_Produit WHERE (Year(Liste_Produit.date_Interaction)=2021 and Liste_Produit.Nom_AS='Test Test' and Month(Liste_Produit.date_Interaction)=1 and Month(Liste_Produit.date_Interaction)=2 and Month(Liste_Produit.date_Interaction)=3 and Month(Liste_Produit.date_Interaction)=4 and Month(Liste_Produit.date_Interaction)=5 and Month(Liste_Produit.date_Interaction)=6)GROUP BY Liste_Produit.[Nom_AS] ORDER BY Liste_Produit.[Nom_AS] PIVOT Liste_Produit.[Produit];
Le graphique ne se met pas à jour et quand j'essaye de mettre directement cette chaine SQL dans le rowSource du graphique ( en manuel ), impossible, j'ai un message d'erreur:
Impossible d'attribuer une valeur à cet objet
Je ne pige pas tout, avez vous une explication ?
Précision:
Quand je créer le graphique Manuellement, dans le champ 'Contenu tranformé' la requete SQL est :
TRANSFORM Count(Liste_Produit.[Produit]) AS CountOfProduit
SELECT Liste_Produit.[Nom_AS]
FROM Liste_Produit
GROUP BY Liste_Produit.[Nom_AS]
ORDER BY Liste_Produit.[Nom_AS]
PIVOT Liste_Produit.[Produit];Ma problématique est d'intégrer la clause "WHERE" de façon dynamique dans cette requête, j'ai fais un essai toujours manuellement en écrivant le plus implement la clause 'WHERE' mais j'ai aussi le meme message d'erreur indiqué dans le poste précédent.
TRANSFORM Count(Liste_Produit.[Produit]) AS CountOfProduit
SELECT Liste_Produit.[Nom_AS]
FROM Liste_Produit
WHERE (Year(Liste_Produit.[Date_Interaction])=2021)
GROUP BY Liste_Produit.[Nom_AS]
ORDER BY Liste_Produit.[Nom_AS]
PIVOT Liste_Produit.[Produit];Je viens de modifier ma procédure 'MAJ_Requete' j'avais fais une erreur entre les 'And' et les 'Or' , voici ma nouvelle procédure:
Private Sub MAJ_Requete()
Dim ch_Mois$, ch_AS$, ch_Produit$, ch_Annee$
Dim chaine, chaineSQL
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
For i = 1 To 12 ' Récupére les Mois sélectionner de type 'int'
If Me.Controls("BP_Mois_" & i) = True Then
If Len(ch_Mois) = 0 Then
ch_Mois = "Month(Liste_Produit.[date_Interaction])=" & i
Else
ch_Mois = ch_Mois & " or Month(Liste_Produit.[date_Interaction])=" & i
End If
End If
Next i
For Each c In BP1_Combo_AS.ItemsSelected ' Récupére les AS sélectionner de type 'String'
If Len(ch_AS) = 0 Then
ch_AS = "Liste_Produit.[Nom_AS]='" & BP1_Combo_AS.ItemData(c) & "'"
Else
ch_AS = ch_AS & " or Liste_Produit.[Nom_AS]='" & BP1_Combo_AS.ItemData(c) & "'"
End If
Next c
For Each c In BP1_Combo_Produit.ItemsSelected ' Récupére les Produits sélectionner de type 'String'
If Len(ch_Produit) = 0 Then
ch_Produit = "Liste_Produit.[Produit]='" & BP1_Combo_Produit.ItemData(c) & "'"
Else
ch_Produit = ch_Produit & " or Liste_Produit.[Produit]='" & BP1_Combo_Produit.ItemData(c) & "'"
End If
Next c
For Each c In Combo_Date.ItemsSelected ' Récupére les Années sélectionner de type 'int'
If Len(ch_Annee) = 0 Then
ch_Annee = "Year(Liste_Produit.[date_Interaction])=" & CInt(Combo_Date.ItemData(c))
Else
ch_Annee = ch_Annee & " or Year(Liste_Produit.[date_Interaction])=" & Combo_Date.ItemData(c)
End If
Next c
liste = Array(ch_Annee, ch_AS, ch_Mois, ch_Produit)
'Création de la clause 'Where' en fonction des champs remplis
For Each c In liste
If Len(c) > 0 Then
If Len(chaine) = 0 Then
chaine = "(" & c & ")"
Else
chaine = chaine & " and " & "(" & c & ")"
End If
End If
Next c
chaineSQL = "TRANSFORM Count(Liste_Produit.[Produit]) AS CountOfProduit " & _
"SELECT Liste_Produit.[Nom_AS] " & _
"FROM Liste_Produit " & _
"WHERE (" & chaine & ")" & _
"GROUP BY Liste_Produit.[Nom_AS] " & _
"ORDER BY Liste_Produit.[Nom_AS] " & _
"PIVOT Liste_Produit.[Produit];"
'Set rs = db.OpenRecordset(chaineSQL)
Debug.Print (chaineSQL)
Me.BP1_View1_Graph1.RowSource = ""
Me.BP1_View1_Graph1.RowSource = chaineSQL
Me.BP1_View1_Graph1.Requery
End SubLa valeur chaineSQL est:
RANSFORM Count(Liste_Produit.[Produit]) AS CountOfProduit SELECT Liste_Produit.[Nom_AS] FROM Liste_Produit WHERE ((Year(Liste_Produit.[date_Interaction])=2021) and (Month(Liste_Produit.[date_Interaction])=1 or Month(Liste_Produit.[date_Interaction])=2 or Month(Liste_Produit.[date_Interaction])=3 or Month(Liste_Produit.[date_Interaction])=4 or Month(Liste_Produit.[date_Interaction])=5 or Month(Liste_Produit.[date_Interaction])=6))GROUP BY Liste_Produit.[Nom_AS] ORDER BY Liste_Produit.[Nom_AS] PIVOT Liste_Produit.[Produit];
Quand j'éxecute manuellement cette requete, elle fonctionne parfaitrement mais impossible de l'envoyer dans le graphique.
Post ouvert également sur un autre forum.
Hello,
"WHERE (" & chaine & ")" & _
"GROUP BY Liste_Produit.[Nom_AS] Tu n'as pas d'espace ici entre ta parenthèse et le G de group by.
C'est identique dans ton prog access ?