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
| Sub Export_AG_EST(requete As String, fichier As String, onglet As String)
Dim xlA As Object, xlW As Object, t As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim db As DAO.Database
Dim s As String, NumChamp As Long, ligne As Long
Set xlA = CreateObject("excel.application") 'lance Excel
xlA.Visible = False
xlA.workbooks.Open (fichier) 'ouvre le fichier
Set xlW = xlA.activeworkbook
ligne = 199
Set db = CurrentDb
Set qdf = db.QueryDefs(requete)
With qdf
.Parameters("annee_mois_debut") = Forms![Export]![annee_mois_debut]
.Parameters("annee_mois_fin") = Forms![Export]![annee_mois_fin]
.Execute End With
Set t = qdf.OpenRecordset 'ouvre la requete
Do Until t.EOF
ligne = ligne + 1 'ligne suivante dans la feuille Excel
For NumChamp = 0 To 1 'pour chaque colonne de la requete
s = t(NumChamp) 'recupération des données au format Texte
xlW.Sheets(onglet).cells(ligne, NumChamp + 1) = s 'ecriture dans la cellule
Next NumChamp
t.MoveNext 'enregistrement suivant
Loop
t.Close
Set t = Nothing
Set qdf = Nothing
Set db = Nothing
xlW.Save
xlA.Quit
Set xlA = Nothing ' puis libère la référence.
End Sub |
Partager