Bonjour,

Suite a la création d'une macro pour charger les data de ma db dans un excel , j'ai voulu automatiser l'envoi d'un mail vers certaines personnes contenue dans une sheet.

Dans la partir rappatrier les données pas de problemes mais des que je fais une query un peu plus spéciale

il ne veut plus boucler :/ et j'obtiens un vilain erreur 91 variable not set


si je fais un select * from table

je n'ai aucun probleme :/

j'avoue patauger un max n'étant pas un VBA addict



si quelqu'un pouvait me guider cela m'aiderait

MErci d'avance



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
Sub Access_Data()
 
    Dim Cn As ADODB.Connection, Rs As ADODB.Recordset
    Dim MyConn, sSQL As String
    Dim Nom, Prenom, search As String
    Dim Rw As Long, Col As Long, c As Long
    Dim MyField, Location As Range
 
    'Set destination
     Set Location = [A1]
    ' Set Location = ThisWorkbook.Worksheets("import").Range("A1")
    ' Set source
     MyConn = "DB.accdb"
 
    For i = 2 To ThisWorkbook.Worksheets("input").Range("M" & Rows.count).End(xlUp).Row
    With test
    Nom = ThisWorkbook.Worksheets("input").Range("M" & i).Value
    Prenom = ThisWorkbook.Worksheets("input").Range("N" & i).Value
    search = UCase(Nom + Prenom)
    End With
 
    sSQL = "SELECT dbo_vw_Persons.int_e_mail_adress, dbo_vw_Persons.refogID FROM dbo_vw_Persons " & _
    "WHERE SEARCH_USUAL_NAME =" & Chr(34) & search & """ AND EXIT_DT IS NULL"
 
    MsgBox ("la query : ") & sSQL & ""
 
    'connection phase & SQL statement
    Set Cn = New ADODB.Connection
    With Cn
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .Open MyConn
        Set Rs = .Execute(sSQL)
    End With
   'Write RecordSet to results area
    Rw = Location.Row
    Col = Location.Column
    c = Col
      Do Until Rs.EOF
        For Each MyField In Rs.Fields
            Cells(Rw, c) = MyField
            MsgBox (" : ") & MyField & ""
            c = c + 1
        Next MyField
        Rs.MoveNext
        Rw = Rw + 1
        c = Col
     Loop
     Set Location = Nothing
     Set Cn = Nothing
    Next i
End Sub