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
| Dim oCnSQLSvr As ADODB.Connection, rsSqlSvr As ADODB.Recordset
Dim rsLst As ADODB.Recordset
Dim tab1() As Variant, i As Integer
Dim strSql As String
' Nouveau recordset ADO
Set rsLst = New ADODB.Recordset
rsLst.CursorLocation = adUseClient
' Crée les champs
rsLst.Fields.Append "CodeClient", adVarChar, 5
rsLst.Fields.Append "Societe", adVarChar, 40
rsLst.Fields.Append "Pays", adVarChar, 15
' Ouvre le recordset sans connexion et sans source
rsLst.Open
' Nouvelle connexion SQL Server
Set oCnSQLSvr = New ADODB.Connection
' Etablit connexion à l'aide d'une chaîne de connexion
oCnSQLSvr.Open "Provider=SQLOLEDB;Data Source=NomServeur\NomInstance;" & _
"Initial Catalog=NomBDD;Integrated Security=SSPI"
' Nouveau recordset pour SQL Server
Set rsSqlSvr = New ADODB.Recordset
' Paramètres du recordset
' (on ne les fournira pas à la méthode open)
rsSqlSvr.CursorLocation = adUseClient
rsSqlSvr.CursorType = adOpenStatic
rsSqlSvr.LockType = adLockReadOnly
Set rsSqlSvr.ActiveConnection = oCnSQLSvr
' Liste des codes clients à chercher
tab1 = Array("BERGS", "BLAUS", "ZZZZZ", "BOLID", "BONAP", "BOTTM", "BSBEV")
For i = LBound(tab1) To UBound(tab1)
' Instruction SQL SELECT sur la table dbo.Clients du serveur SQL Server
strSql = "SELECT CodeClient, Societe, Pays FROM dbo.Clients " & _
"WHERE CodeClient='" & tab1(i) & "'"
' Ouvre recordset sur Instruction SQL SELECT
rsSqlSvr.Open strSql
If Not rsSqlSvr.EOF Then
' Ajoute un enregistrement au recordset 'sans source'
rsLst.AddNew
rsLst.Fields("CodeClient") = rsSqlSvr.Fields("CodeClient")
rsLst.Fields("Societe") = rsSqlSvr.Fields("Societe")
rsLst.Fields("Pays") = rsSqlSvr.Fields("Pays")
rsLst.Update
End If
rsSqlSvr.Close
Next
'Fermeture et libération des variables objets
Set rsSqlSvr = Nothing
oCnSQLSvr.Close
Set oCnSQLSvr = Nothing |
Partager