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
| 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"
'connection phase & SQL statement
Set Cn = New ADODB.Connection
With Cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open MyConn
End With
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 & ""
Set Rs = Cn.Execute(sSQL)
'Write RecordSet to results area
Rs.MoveFirst
Rw = Location.Row
Col = Location.Column
c = Col
Do
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 While Rs.EOF
Next i
Set Location = Nothing
Set Cn = Nothing
Set Rs = Nothing
End Sub |