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
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
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
 
Upvote 0

Forum statistics

Threads
1,214,883
Messages
6,122,077
Members
449,064
Latest member
MattDRT

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
Back
Top