exporting data

cahickm2

Board Regular
Joined
May 14, 2007
Messages
101
I need help on exporting data from a query into a specific wooksheet in excel.

ex. I want all data from "query 1" to be impoted onto "sheet1" in excel
 

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

sll810

Board Regular
Joined
Jun 29, 2007
Messages
86
sub open_query ()
Dim db As DAO.Database
Dim rs, rs2, rs3 As DAO.Recordset
Dim qry, dbqry, dbqry2 As DAO.QueryDef
Dim xlapp as excel.application

Set db = Access.CurrentDb
Set qry = db.QueryDefs("Query1")
Set rs = qry.OpenRecordset

call CopyRecordSet(rs)

end sub

Sub CopyRecordSet(InRecSet As Recordset)


Dim xlapp As Excel.Application, xlNewBook As Excel.Workbook, xlDestSheet, xlDestSheet2 As Excel.Worksheet
Dim fld As DAO.Field
Dim i As Integer

'open excel
Set xlapp = New Excel.Application
xlapp.Visible = True

'add new sheets for each recordset
Set xlNewBook = xlapp.Workbooks.Add
Set xlDestSheet = xlNewBook.Worksheets.Add

'copy query into excel sheet
i = 1
For Each fld In InRecSet.Fields
xlDestSheet.Cells(1, i).Value = fld.Name
i = i + 1
Next fld
xlDestSheet.Range("A2").CopyFromRecordset InRecSet
xlDestSheet.Name = ("Data")

End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,123,514
Messages
5,602,093
Members
414,501
Latest member
mdhaumyu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top