Remplir un Excel avec une requete (Please Help Me !!!)

mayerp Messages postés 14 Date d'inscription mercredi 21 janvier 2004 Statut Membre Dernière intervention 16 juin 2004 - 16 juin 2004 à 16:33
conseildg Messages postés 86 Date d'inscription lundi 16 décembre 2002 Statut Membre Dernière intervention 18 mars 2007 - 17 juin 2004 à 18:55
Salut,

j'ai une requete de selection sous access et j'aimerai afficher le resultat dans un excel.
Je ne veux pas remplir cellule par cellule car ce serai trop long.
Je crois qu'il faut utiliser la cmd OutputTo mais bon...

Quelqu'un pourrait-il m'aider ?

Merci pour vos réponses !

2 réponses

carnez Messages postés 120 Date d'inscription jeudi 31 octobre 2002 Statut Membre Dernière intervention 2 août 2004
17 juin 2004 à 15:28
tu affiches le résultat de ta requête sélection puis immédiatement :

docmd.runcommand acCmdOutputToExcel

Colibri
0
conseildg Messages postés 86 Date d'inscription lundi 16 décembre 2002 Statut Membre Dernière intervention 18 mars 2007
17 juin 2004 à 18:55
si j'ai bien compris la question , essayer ce code :

Set ApExcel = CreateObject("Excel.application") 'Creates an object

ApExcel.Visible = True 'This enable you to see the process in Excel
ApExcel.Workbooks.Add 'Adds a new book.

'Set the connection string
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBAccess
'Open the connection
cn.Open

'Check that the connection is open
If cn.State = 0 Then cn.Open
Set cmd.ActiveConnection = cn
cmd.CommandText = DBTable
cmd.CommandType = adCmdTable
Set rs = cmd.Execute
'Count the number of fields or column
MyFieldCount = rs.Fields.Count

'Fill the first line with the name of the fields
For MyIndex = 0 To MyFieldCount - 1
ApExcel.Cells(InitRow, (MyIndex + 1)).Formula = rs.Fields(MyIndex).Name 'Write Title to a Cell
ApExcel.Cells(InitRow, (MyIndex + 1)).Font.Bold = True
ApExcel.Cells(InitRow, (MyIndex + 1)).interior.colorindex = 36
ApExcel.Cells(InitRow, (MyIndex + 1)).WrapText = True
Next
'Fill the excel book with the values from the database
Do While rs.EOF = False
For MyIndex = 1 To MyFieldCount
ApExcel.Cells(MyRecordCount, MyIndex).Formula = rs((MyIndex - 1)).Value 'Write Value to a Cell
ApExcel.Cells(MyRecordCount, MyIndex).WrapText = False 'Format the Cell
Next
MyRecordCount = MyRecordCount + 1
rs.MoveNext
If MyRecordCount > 50 Then
Exit Do
End If
Loop
'Return the last position in the workbook
Export2XL = MyRecordCount

ceci suppose que les variables soient définies avant.
0
Rejoignez-nous