Bonjour,

Je cherche à récupérer le nombre de lignes d'une requête SQL, le tout en VBA.
J'ai essayé d'utiliser un RecordCount, cependant il me renvoie systématiquement -1...

En espérant avoir une lumière sur ce RecordCount qui me pose un problème...

EDIT :

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
Sub CompleteTableauCommandeProvisoire()
 
    'Déclaration des variables
    Dim cnx As ADODB.Connection
    Dim rst As ADODB.Recordset
 
    'Instanciation des variables
    Set cnx = New ADODB.Connection
    Set rst = New ADODB.Recordset
 
    Dim sql1 As String
    Dim debutperiode As Date
    Dim ddeb As String
    Dim dfin As String
    Dim Ligne As Integer
    Dim j As Integer
    Dim result_sql
    Dim Col
    Dim cptLigne As Integer
 
    cnx.ConnectionString = "DSN=Stats;UID=***;PWD=***;"
    cnx.Open
 
    cptLigne = 6
 
    'Changer la date de début d'année
    For cptLigne = 6 To 263
        For debutperiode = DateSerial(2008, 12, 29) To Now() - 6 Step 7
            ddeb = Format(debutperiode, "dd/mm/yyyy")
            dfin = Format(debutperiode + 6, "dd/mm/yyyy")
            'Nombre total d'envois périodique (courrier ):
             sql1 = "select m.mp_l, count(*), sum(c.cde_tot_ttc)" + _
                " from   e_cde c, e_mode_paiement m" + _
                " where  c.cde_ty_se_c = 'WV2'" + _
                " and    c.cde_mp_c in ('KM','KI','KW','KT', 'KA','KC', 'CA')" + _
                " and    c.cde_mp_c = m.mp_c" + _
                " and    c.cde_d between" + _
                " to_date('" + ddeb + " 00:00:00' , 'dd/mm/yyyy hh24:mi:ss')" + _
                " and to_date('" + dfin + " 23:59:59', 'dd/mm/yyyy hh24:mi:ss') " + _
                " group by m.mp_l" + _
                " order by 1"
 
            rst.Open sql1, cnx
            rst.MoveFirst
            j = 0
 
            result = rst.RecordCount
 
 
            Do
                For Ligne = cptLigne To cptLigne + result
                    For Col = 65 To 67
                    'Lecture en ligne
                    Range(Chr(Col) & Ligne).Select
                    'Récupère résultat
                    result_sql = rst.Fields(j).Value
                    'Transfert le résultat dans la cellule
                    ActiveCell.FormulaR1C1 = result_sql
                    j = j + 1
                    Next
                j = 0
                rst.MoveNext
                Next
            Loop While rst.BOF
            cptLigne = cptLigne + 12
            rst.Close
 
        Next
    Next
End Sub

Cordialement,
Clément.