How do i send query output to specific excel sheet?

hardrock

Registered User.
Local time
Today, 09:38
Joined
Apr 5, 2007
Messages
166
Hi all, could someone please advise me how to send the records returned from a query to a specific sheet in an excel spreadsheet using VBA if possible.

The bit of code below will create all the sheets i need, but it's missing the code to paste the query result to selected sheet. I hope i've explained it clearly. Any help appreciated. thanks


Set db = CurrentDb

' All distinct supplier names in this query
Set qd = db.QueryDefs("Distinct_Supplier") For Each Param In

qd.Parameters
Param.Value = Eval(Param.Name)
Next Param
DoCmd.SetWarnings True
Set rs = qd.OpenRecordset()
While Not rs.EOF And Not rs.BOF
'*************************************************
Me.sup = rs!SupplierName
'************************
DoEvents
xlapp.Visible = False
xlapp.Workbooks.Open (ExportDir & ExportFile)
xlapp.DisplayAlerts = False


On Error Resume Next
DoCmd.SetWarnings False
xlapp.Sheets.Add.Name = sup 'Adds the sheets to spreadsheet

****************************
Missing code to select sheet and paste query result goes here?

****************************

xlapp.SaveWorkspace
xlapp.Workbooks.Close
xlapp.Application.Quit
Set xlapp = Nothing

rs.MoveNext
Wend
 
Thanks, and very usefull, however still struggling to get the query to output to the specific sheet.

Have tried

DoCmd.OutputTo acOutputQuery, "Distinct_Result", acFormatXLS, ExportDir & ExportFile, True, "A:J"

and

'DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Distinct_Result", ExportDir & ExportFile, True, "A:J"

What i am trying to do is an "Apend" to an existing spreadsheet,so i dont think the transferspreadsheet is an option here! Still stuck :(
 

Users who are viewing this thread

Back
Top Bottom