Problème de requete sql dans une macro vba

Résolu
GarnierFlorian Messages postés 129 Date d'inscription mardi 27 mai 2008 Statut Membre Dernière intervention 26 juillet 2011 - 12 nov. 2010 à 09:30
GarnierFlorian Messages postés 129 Date d'inscription mardi 27 mai 2008 Statut Membre Dernière intervention 26 juillet 2011 - 12 nov. 2010 à 11:44
Bonjour à tous !

Mon problème se situe, je pense, au niveau de mes paramètres que je passe pour exécuter ma requète. Ce que je souhaite faire, récupérer toutes les infos qui correspondent au mois et l'année récupérer dans ma form !

Je vous met directement tout mon code, depuis le début de mon module, ce n'est pas très long :

Sub Recuperation()
    Dim ss_cmd As ADODB.Command
    Dim ss_parmoi As ADODB.Parameter
    Dim ss_parann As ADODB.Parameter
    Dim ss_rst As ADODB.Recordset
    
    On Error GoTo Recuperation_Error
        'Fonction permettant de se connecter aux tables foxpro
        Connexion

        If FLGCNX = True Then

            'On déclare la command
            Set ss_cmd = New ADODB.Command
            With ss_cmd
                .ActiveConnection = CNXFOX
                .CommandType = adCmdText
                .NamedParameters = True
            End With
            
            Set ss_parmoi = New ADODB.Parameter
            Set ss_parann = New ADODB.Parameter
            
            With ss_parmoi
                .Direction = adParamInputOutput
                .Type = adInteger
                .Name = "ChoixMois"
            End With
            
            With ss_parann
                .Direction = adParamInputOutput
                .Type = adInteger
                .Name = "ChoixAnnee"
            End With

            'SelectionRequeteProspect
            ss_cmd.Parameters.Append ss_parmoi
            ss_cmd.Parameters.Append ss_parann
                        
            ss_parmoi.Value = CInt(RPT.MOI)
            ss_parann.Value = CInt(RPT.ANN)

            RESEXE.REQRPT = "SELECT DISTINCT pays.nompay,COUNT(client.numctr) AS nbprospect" & _
                                " FROM pays,client" & _
                                " WHERE client.codpay = pays.codpay" & _
                                " AND flgprp = 1" & _
                                " AND MONTH(client.datcre) = " & ss_parmoi & _
                                " AND YEAR(client.datcre) = " & ss_parann & _
                                " GROUP BY pays.nompay" & _
                                " ORDER BY pays.nompay"

            ss_cmd.CommandText = "PARAMETERS ss_parmoi Integer ,ss_parann Integer;" & RESEXE.REQRPT

            'On déclare le recordset
            Set ss_rst = New ADODB.Recordset
            'Exécute la requête
            With ss_rst
                .CursorLocation = adUseClient
                .CursorType = adOpenStatic
                .LockType = adLockReadOnly
            End With
            
            ss_rst.Open ss_cmd            
           
            'Fonctions/procédures diverses

            ss_rst.Close
                
            Set ss_cmd = Nothing
            Set ss_rst = Nothing
        End If
        
        'Fonction permettant de se déconnecter aux tables foxpro
        Deconnexion
   On Error GoTo 0
   Exit Sub

Recuperation_Error:
    MsgBox "(Erreur n°" & ERR.Number & ") " & ERR.Description
    RESEXE.ERR = ERR.Description
    FLGERR = True
End Sub


Et donc, sur cette ligne,
ss_rst.Open ss_cmd 
,
j'ai droit au message "Syntax Error" !

Donc voilà, si quelqu'un à une petite idée, elle est la bienvenue :)

Et juste une petite précision, j'ai l'impression que mes fonctions CInt ne convertissent pas ma chaine en integer, donc peut-être mon problème viendrait-il de la ?

Merci d'avance !

Peace

 

4 réponses

GarnierFlorian Messages postés 129 Date d'inscription mardi 27 mai 2008 Statut Membre Dernière intervention 26 juillet 2011
12 nov. 2010 à 11:44
(Désolé pour le double post :/)

Et donc : j'ai juste modifier cette petite partie :

ss_cmd.CommandText = "PARAMETERS ss_parmoi Integer ,ss_parann Integer;" & RESEXE.REQRPT


Vers :
 ss_cmd.CommandText = RESEXE.REQRPT


Je m'étais basé sur une procédure trouvé sur un tuto sur le net, mais je pense que cette partie ne devait pas être bonne, ou alors je l'utilise mal ??!

Peace
 
3
GarnierFlorian Messages postés 129 Date d'inscription mardi 27 mai 2008 Statut Membre Dernière intervention 26 juillet 2011
12 nov. 2010 à 11:30
Bon, je viens d'effectuer une modification :

J'ai remplacé
.CommandType = adCmdText


par
.CommandType = adCmdStoredProc


étant donné que ce n'estp as qu'une simple requete sql, à cause des paramètres !

Du coup, je n'ai pu le meme message d'erreur qui devient : "Function name is missing )."

Donc voila, j'attends votre aide pour savoir si j'ai bien fait de modifier mon type de commande, et savoir d'ou peut venir l'erreur ?!

Peace


 
0
GarnierFlorian Messages postés 129 Date d'inscription mardi 27 mai 2008 Statut Membre Dernière intervention 26 juillet 2011
12 nov. 2010 à 11:37
Je reviens sur mon dernier message :

adCmdText : CommandText correspond à la définition textuelle d'une commande ou d'un appel de procédure stockée


adCmdStoredProc : CommandText correspond au nom d'une procédure stockée


Donc je suis revenu sur mon adCmdText, qui je pense fonctionne !

Peace
0
GarnierFlorian Messages postés 129 Date d'inscription mardi 27 mai 2008 Statut Membre Dernière intervention 26 juillet 2011
12 nov. 2010 à 11:42
Bon, ba ça marche ?!?!

Je ne sais pas trop pourquoi ça ne marchait pas avant :

Sub Recuperation()
    Dim ss_cmd As ADODB.Command
    Dim ss_parmoi As ADODB.Parameter
    Dim ss_parann As ADODB.Parameter
    Dim ss_rst As ADODB.Recordset
    
    On Error GoTo Recuperation_Error
    
        Connexion

        If FLGCNX = True Then
            RESEXE.X = 13
            RESEXE.Y = 3
            'On déclare la command
            Set ss_cmd = New ADODB.Command
            With ss_cmd
                .ActiveConnection = CNXFOX
                .CommandType = adCmdText
                .NamedParameters = True
            End With
            
            Set ss_parmoi = New ADODB.Parameter
            Set ss_parann = New ADODB.Parameter
            
            With ss_parmoi
                .Direction = adParamInputOutput
                .Type = adInteger
                .Name = "ChoixMois"
            End With
            
            With ss_parann
                .Direction = adParamInputOutput
                .Type = adInteger
                .Name = "ChoixAnnee"
            End With
            
            ss_cmd.Parameters.Append ss_parmoi
            ss_cmd.Parameters.Append ss_parann
                        
            ss_parmoi.Value = CInt(RPT.MOI)
            ss_parann.Value = CInt(RPT.ANN)

            RESEXE.REQRPT = "SELECT DISTINCT pays.nompay,COUNT(client.numctr) AS nbprospect" & _
                                " FROM pays,client" & _
                                " WHERE client.codpay = pays.codpay" & _
                                " AND flgprp = 1" & _
                                " AND MONTH(client.datcre) = " & ss_parmoi & _
                                " AND YEAR(client.datcre) = " & ss_parann & _
                                " GROUP BY pays.nompay" & _
                                " ORDER BY pays.nompay"

'                                "

            ss_cmd.CommandText = RESEXE.REQRPT

            'On déclare le recordset
            Set ss_rst = New ADODB.Recordset
            'Exécute la requête
            With ss_rst
                .CursorLocation = adUseClient
                .CursorType = adOpenStatic
                .LockType = adLockReadOnly
            End With
            
            ss_rst.Open ss_cmd
 
            ss_rst.Close
                
            Set ss_cmd = Nothing
            Set ss_rst = Nothing
        End If
        
        Deconnexion
   On Error GoTo 0
   Exit Sub

Recuperation_Error:
    MsgBox "(Erreur n°" & ERR.Number & ") " & ERR.Description
    RESEXE.ERR = ERR.Description
    FLGERR = True
End Sub



 
0
Rejoignez-nous