• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

Output Access query to Excel

I have seen numerous posts talking about how to save access query to an excel file.

I'm wondering if it is possible to output access query output to excel without saving it to a file.

I.e an instance of excel on my screen
  • 3
  • 2
  • 2
2 Solutions

Yes - this can be done for example; the following code shows you how to write values to an excel sheet; if you want to do fancy things - record a macro in excel and then copy the code and insert it into the VBA code.  As you can reference the excel sheet by ordinal position ie (1,2) you can loop through a recordset and write the values.

there is no way to actually do it through something like acTransferText - as this will save - so you have to write the values yourself; the advantage is however you can create multiple worksheets and once you have a generic function you can pass all of your queries to it.

'************ Code Start **********
'This code was originally written by Dev Ashish
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'Code Courtesy of
'Dev Ashish
Sub sTestXL()
Dim objXL As Object
Dim strWhat As String, boolXL As Boolean
Dim objActiveWkb As Object

    If fIsAppRunning("Excel") Then
        Set objXL = GetObject(, "Excel.Application")
        boolXL = False
        Set objXL = CreateObject("Excel.Application")
        boolXL = True
    End If
    Set objActiveWkb = objXL.Application.ActiveWorkBook
    With objActiveWkb
        .Worksheets(1).Cells(1, 1) = "Hello World"
        strWhat = .Worksheets(1).Cells(1, 1).value
    End With
    objActiveWkb.Close savechanges:=False
    If boolXL Then objXL.Application.Quit
    Set objActiveWkb = Nothing: Set objXL = Nothing
    MsgBox strWhat
End Sub
'************ Code End **********
Yes u can do exactly what u want. Its Excel Automation that is required as suggested by Raynard7

Here is sample code that opens excel, dumps the result of your query and leaves it open. It does not save to a file.
It makes use of CopyFromRecordset

Public Sub CreateExcelWBFromQuery()

    Dim sSql As String
    Dim objXL As Object
    Dim objWB As Object
    Dim rs As DAO.Recordset
    'Open a recordset for your query
    sSql = "SELECT * FROM Table3"
    Set rs = CurrentDb.OpenRecordset(sSql)
    'Create a new excel document
    Set objXL = CreateObject("Excel.Application")
    'To create new workbook
    Set objWB = objXL.Workbooks.Add
    'Paste the values from your query starting from A1
    objWB.Sheets(1).Range("A1").CopyFromRecordset rs

    'Show the user your glorious results
    objXL.Visible = True

     set rs=Nothing
End Sub

Note, if u do not have DAO setup as an object reference, you need to as that is what I use for getting results from a query.
Go to Tools/References and check the latest version.

johnnyg123Author Commented:

Is it possible to use open record with a "predefined" query ?

I could copy the sql but would prefer just to use existing query if possible
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

u could try this

dim qdf as dao.querydef

set qdf = currentdb.querydefs("nameofyourquery")

Set rs = CurrentDb.OpenRecordset(qdf.sql)

I agree with rockiroads - you can always use a queryDef instead of sql;

You can also just substitute the name in th reordset name ie;
Set rs = CurrentDb.OpenRecordset("nameofyourquery")
johnnyg123Author Commented:
Thanks for the input!

I will split the points
No probs. Glad to have helped
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now