Solved

Output Access query to Excel

Posted on 2006-06-29
7
283 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:johnnyg123
  • 3
  • 2
  • 2
7 Comments
 
LVL 35

Assisted Solution

by:Raynard7
Raynard7 earned 250 total points
Comment Utility
Hi,

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
    Else
        Set objXL = CreateObject("Excel.Application")
        boolXL = True
    End If
   
    objXL.Application.workbooks.Add
    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 **********
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 250 total points
Comment Utility
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

     rs.Close    
     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.



0
 

Author Comment

by:johnnyg123
Comment Utility
rockiroads,

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
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
u could try this

dim qdf as dao.querydef


set qdf = currentdb.querydefs("nameofyourquery")

Set rs = CurrentDb.OpenRecordset(qdf.sql)

0
 
LVL 35

Expert Comment

by:Raynard7
Comment Utility
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")
0
 

Author Comment

by:johnnyg123
Comment Utility
Thanks for the input!


I will split the points
0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
No probs. Glad to have helped
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now