1

I've been trying to use transfer spreadsheet methods but they appear to require an output path.

I just need to find out how to take a given query and simply "open up" an Excel file that contains the query output. I don't need the file actually saved anywhere.

3 Answers 3

1

You can open up your file without saving it by creating an Excel instance (or grabbing an existing one) and using the CopyFromRecordset function of the Excel.Range object.

This assumes your data are in an ADO recordset. You need to have references to Microsoft Excel XX.0 Object Library and Microsoft ActiveX Data Objects X.X Library` (if you are using ADO. If you use DAO then use whatever DAO reference you need)

I use this to grab an an Excel app or create a new one is Excel is not open already. I use WasANewInstanceReturned to figure how I need to clean up the Excel resources at the end. (Obviously I don't want to quit Excel if it is being use by something else).

Function GetExcelApplication(Optional ByRef WasANewInstanceReturned As Boolean) As Excel.Application

    If ExcelInstanceCount > 0 Then
        Set GetExcelApplication = GetObject(, "Excel.Application")
        WasANewInstanceReturned = False
    Else
        Set GetExcelApplication = New Excel.Application
        WasANewInstanceReturned = True
    End If
End Function

Then grab that instance

Dim ApXL As Excel.Application, WasANewInstanceReturned as Boolean
Set ApXL = GetExcelApplication(WasANewInstanceReturned)

Add a workbook

Dim wbExp As Excel.Workbook
Set wbExp = ApXL.Workbooks.Add

Grab the first sheet

Dim wsSheet1 As Excel.Worksheet
Set wsSheet1 = wbExp.Sheets(1)

Put your recordset's field names in the first row

Dim fld As ADODB.Field
Dim col As Integer
col = 1
With wsSheet1 
For Each fld In rst.Fields
    .Cells(1, col).Value = fld.Name  'puts the field names in the first row
    End With
    col = col + 1
Next fld
End With

Then move the data just below the field names

wsSheet1 .Range("A2").CopyFromRecordset rst

Voila! You have an excel file open, with your data that has not been saved anywhere!

I usually set ApXL.ScreenUpdating = False before doing any of this and ApXL.ScreenUpdating = True at the end.

I'll let you stitch this together for your needs.

Sign up to request clarification or add additional context in comments.

Comments

0

The file must be saved somewhere for Excel to open it.
If the dataset is small enough, you can use copy/paste (no file here). Otherwise, just use the %TEMP% folder for the file location.


Edit:
One simple way to get the TEMP folder is to use =Environ("TEMP")

1 Comment

How do I use the temp folder properly?
0

I open and export a query from access to excel. First I created a worksheet in excel and saved it. Then I created a module in the vba part of Access (2013):

Option Compare Database


' Testtoexporttoexcel'

Function ExportQuerytoExcel()

On Error GoTo ExportQuerytoExcel_Err

' Exports the query to excel to a sheet named Nameofyoursheet

    DoCmd.TransferSpreadsheet acExport, 10, "nameofyourquery", "yourPath:\nameofyourworkbook", False, "Nameofyour worksheet"


ExportQuerytoExcel_Exit:
    Exit Function

ExportQuerytoExcel_Err:

    MsgBox Error$

    Resume ExportQuerytoExcel_Exit

End Function

-----then add another function that says:

Option Compare Database

Function OpenExcelFromAccess()

'Opens Excel to the chart

 Dim MYXL As Object

 Set MYXL = CreateObject("Excel.Application")

  With MYXL

  .Application.Visible = True

  .workbooks.Open "Yourpath:\nameofyourworkbook"

  End With

  'Application.Quit

End Function

hope this helps, this is my first time answering a question. Aloha

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.