VBA/SQL - Manipulation d'un recordset
Bonjour,
mon problème est plutôt simple et ma question est plus d'un ordre logique. Elle concerne vba excel et sql.
J'ai besoin d'importer des données sql dans une feuille. J'ai bien ma requête sql, qui me retourne tout ce dont j'ai besoin MAIS j'ai besoin d'inserer entre certaine des colonnes de mon recordset, d'autre colonnes : en d'autre termes, j'ai besoin de selectionner une colonne de mon recordset, de l'inserer à tel ou tel endroit de ma feuille.
Ma question est donc, comment selectionner une colonne d'un recordset et la coller où je le désire? Est-ce que j'envisage le problème de la bonne manière ou il faut procéder différement?
Code:
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
| Sub sql_import_ORA()
'Requis:
'Menu: Tools/References (Microsoft ActiveX Data Objects 2.8 Library)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
Worksheets("Imports").Delete
Worksheets.Add().Name = "Imports"
ActiveSheet.Select = "Imports"
'
'*----------------------------*
'* Requête SQL *
'*----------------------------*
'Declare variables
Set objMyConn = New ADODB.Connection
Set objMyCmd = New ADODB.Command
Set rs = New ADODB.Recordset
'Open Connection
objMyConn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=kofvmmssql021\sql071;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=KOL60404;Use Encryption for Data=False;Tag with Column collation when possible=False"
objMyConn.Open
Set objMyCmd.ActiveConnection = objMyConn
objMyCmd.CommandType = adCmdText
Dim sql As String
sql = "select TRAN_NUMERO_SERIE,TRAN_INDICE_CANAL, MODELE,TRAN_TELEPHONE,ESI_ADRESSE"
sql = sql & ",ESI_CODEPOSTAL,ESI_VILLE"
sql = sql & " from METIERS.dbo.AMI_TS_PARC"
sql = sql & " where MODELE like '%amph%' and modele not like '%amphresi%'"
objMyCmd.CommandText = sql
objMyCmd.Execute
'Open Recordset
Set rs.ActiveConnection = objMyConn
rs.Open objMyCmd
'Copy Data to Excel
'pour l'instant je fais comme ça, ce qui me copie tout le recordset
'ActiveSheet.Range("A2").CopyFromRecordset (rs)
'je voudrais être capable de selectionner une colonne un peu comme ça
ActiveSheet.Range("A2").Value = rs!TRAN_NUMERO_SERIE.Value
'sauf que ça ne me retourne qu'une ligne
'Close Connection
objMyConn.Close
Set objMyConn = Nothing
Set rs = Nothing
End Sub |
D'avance merci.