Comment optimiser ce bout de code pour une exécution plus rapide

Résolu/Fermé
basthed Messages postés 10 Date d'inscription jeudi 12 mars 2020 Statut Membre Dernière intervention 31 juillet 2021 - Modifié le 9 avril 2020 à 15:04
yg_be Messages postés 22707 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 19 avril 2024 - 9 avril 2020 à 17:49
Bonjour j’exécute ce bout de code qui marche bien mais trop lent est ce qu'il y'aurai un moyen de l'écrire pour un résultat plus rapide?

Private Sub RefreshQuery()
Child29!TXT_FILTRE_TRANSPO2 = ""
Child29!TXT_NB_MAILS.Value = 0
Dim SQL As String
Dim SQLWhere As String

' cette variable permet de s'assurer qu'une selection est active avant d'effectuer la requete
Dim CompteurSelections As Integer
CompteurSelections = 0
If (Me.chkClient) And (Me.cmbRechClient.ListIndex <> -1) Then
    CompteurSelections = CompteurSelections + 1
End If
If (Me.chkTransporteur) And (Me.cmbTransporteur <> -1) Then
    CompteurSelections = CompteurSelections + 1
End If
If (Me.chkCodEnlevement) And (Me.cmbCodEnlevement.ListIndex <> -1) Then
    CompteurSelections = CompteurSelections + 1
End If
If (Me.chkCodLivraison) And (Me.cmbCodLivraison.ListIndex <> -1) Then
    CompteurSelections = CompteurSelections + 1
End If
If (Me.chkPaysEnlev) And (Me.cmbPaysEnlev.ListIndex <> -1) Then
    CompteurSelections = CompteurSelections + 1
End If
If (Me.chkPaysLiv) And (Me.cmbPaysLiv.ListIndex <> -1) Then
    CompteurSelections = CompteurSelections + 1
End If
If (Me.chkEnlevDateDebut) And (Me.txtEnlevDateDebut.Value <> "") Then
    CompteurSelections = CompteurSelections + 1
End If
If (Me.chkEnlevDateDebut) And (Me.txtEnlevDateFin.Value <> "") Then
    CompteurSelections = CompteurSelections + 1
End If
If (Me.chkLivDateDebut) And (Me.txtLivDateDebut.Value <> "") Then
    CompteurSelections = CompteurSelections + 1
End If
If (Me.chkLivDateDebut) And (Me.txtLivDateFin.Value <> "") Then
    CompteurSelections = CompteurSelections + 1
End If
If (Me.chkUtilisateur) And (Me.cmbRechUtilisateur.ListIndex <> -1) Then
    CompteurSelections = CompteurSelections + 1
End If

If CompteurSelections > 0 Then
    SQL = "SELECT * FROM [Liste_par_client_R] where [Liste_par_client_R].[No Client] <> 0 "

    If (Me.chkClient) And (Me.cmbRechClient.ListIndex <> -1) Then
        SQL = SQL & " And [Liste_par_client_R].[No Client] = " & Me.cmbRechClient & ""
    End If
    
    If (Me.chkTransporteur) And (Me.cmbTransporteur <> -1) Then
        SQL = SQL & " And [Liste_par_client_R].[No Transporteur] = " & Me.cmbTransporteur & ""
    End If
    If (Me.chkCodEnlevement) And (Me.cmbCodEnlevement.ListIndex <> -1) Then
        SQL = SQL & " And [Liste_par_client_R].[EnlevCode] = " & Me.cmbCodEnlevement & ""
    End If
    If (Me.chkCodLivraison) And (Me.cmbCodLivraison.ListIndex <> -1) Then
        SQL = SQL & " And [Liste_par_client_R].[LivCode] = " & Me.cmbCodLivraison & ""
    End If
    If (Me.chkPaysEnlev) And (Me.cmbPaysEnlev.ListIndex <> -1) Then
        SQL = SQL & " And [Liste_par_client_R].[EnlevPays] = " & "'" & Me.cmbPaysEnlev & "'" & ""
    End If
    If (Me.chkPaysLiv) And (Me.cmbPaysLiv.ListIndex <> -1) Then
        SQL = SQL & " And [Liste_par_client_R].[LivPays] = " & "'" & Me.cmbPaysLiv & "'" & ""
    End If
    
    If (Me.chkEnlevDateDebut) And (Me.txtEnlevDateDebut.Value <> "") Then
        SQL = SQL & " And [Liste_par_client_R].[EnlevDate] >= DateValue('" & Me.txtEnlevDateDebut & "')" & ""
    End If
    If (Me.chkEnlevDateDebut) And (Me.txtEnlevDateFin.Value <> "") Then
        SQL = SQL & " And [Liste_par_client_R].[EnlevDate] <= DateValue('" & Me.txtEnlevDateFin & "')" & ""
    End If
    
    If (Me.chkLivDateDebut) And (Me.txtLivDateDebut.Value <> "") Then
        SQL = SQL & " And [Liste_par_client_R].[LivDate] >= DateValue('" & Me.txtLivDateDebut & "')" & ""
    End If
    If (Me.chkLivDateDebut) And (Me.txtLivDateFin.Value <> "") Then
        SQL = SQL & " And [Liste_par_client_R].[LivDate] <= DateValue('" & Me.txtLivDateFin & "')" & ""
    End If
    
    
    If (Me.chkUtilisateur) And (Me.cmbRechUtilisateur.ListIndex <> -1) Then
        SQL = SQL & " And [Liste_par_client_R].[Utilisateur] = '" & Me.cmbRechUtilisateur & "'"
    End If
    
    SQLWhere = Trim(Right(SQL, Len(SQL) - InStr(SQL, "Where ") - Len("Where ") + 1))
    
    SQL = SQL & " order by [EnlevDate] desc;"

End If

Me.lblStats.Caption = DCount("*", "Liste_par_client_R")
'Me.lblStats.Caption = DCount("*", "Liste_par_client_R", SQLWhere) & " / " & DCount("*", "Liste_par_client_R")
' MsgBox "requete: " & SQL
Me!Child29.Form.RecordSource = SQL



End Sub
A voir également:

2 réponses

yg_be Messages postés 22707 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 19 avril 2024 1 471
Modifié le 9 avril 2020 à 15:39
bonjour, c'est peut-être la requête qui prend trop de temps.
le temps dépend-il des sélections faites?
peux-tu récupérer la requête SQL et l'exécuter à la main?

je ne comprends pas pourquoi tu fais tous ces tests deux fois, mais cela ne devrait pas influencer le temps d'exécution.
0
basthed Messages postés 10 Date d'inscription jeudi 12 mars 2020 Statut Membre Dernière intervention 31 juillet 2021
9 avril 2020 à 17:34
Salut j'ai exécuté la requête à la main et tu as parfaitement vu juste elle prend beaucoup de temps pour s’exécuter.

Requete

SELECT * FROM [Liste_par_client_R] where [Liste_par_client_R].[No Client] <> 0

est ce qu'il y'aurais pas un moyen de faire mieux?
0
yg_be Messages postés 22707 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 19 avril 2024 1 471
9 avril 2020 à 17:49
peut-être en indexant la table?
0