简体   繁体   English

MS Access VBA将查询输出转换为Excel格式,但不保存在任何地方

[英]MS Access VBA convert query output to Excel format but not saving anywhere

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. 我只需要找出如何进行给定查询,然后简单地“打开”包含查询输出的Excel文件即可。 I don't need the file actually saved anywhere. 我不需要实际保存在任何地方的文件。

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. 您可以通过创建Excel实例(或获取现有实例)并使用Excel.Range对象的CopyFromRecordset函数来打开文件而不保存文件。

This assumes your data are in an ADO recordset. 假定您的数据在ADO记录集中。 You need to have references to Microsoft Excel XX.0 Object Library and Microsoft ActiveX Data Objects XX Library` (if you are using ADO. If you use DAO then use whatever DAO reference you need) 您需要具有对Microsoft Excel XX.0 Object Library和Microsoft ActiveX数据对象XX库`的引用(如果使用的是ADO。如果使用的是DAO,请使用所需的任何DAO引用)

I use this to grab an an Excel app or create a new one is Excel is not open already. 我用它来抓取一个Excel应用程序或创建一个新的Excel应用程序,因为Excel尚未打开。 I use WasANewInstanceReturned to figure how I need to clean up the Excel resources at the end. 我使用WasANewInstanceReturned来弄清楚最后需要如何清理Excel资源。 (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! 您打开了一个excel文件,其中的数据尚未保存在任何地方!

I usually set ApXL.ScreenUpdating = False before doing any of this and ApXL.ScreenUpdating = True at the end. 通常,在执行任何此操作之前,我通常设置ApXL.ScreenUpdating = FalseApXL.ScreenUpdating = True

I'll let you stitch this together for your needs. 我将根据您的需要将它们缝在一起。

The file must be saved somewhere for Excel to open it. 必须将文件保存在Excel的某个位置才能打开它。
If the dataset is small enough, you can use copy/paste (no file here). 如果数据集足够小,则可以使用复制/粘贴(此处没有文件)。 Otherwise, just use the %TEMP% folder for the file location. 否则,只需使用%TEMP%文件夹作为文件位置。


Edit: 编辑:
One simple way to get the TEMP folder is to use =Environ("TEMP") 获取TEMP文件夹的一种简单方法是使用=Environ("TEMP")

I open and export a query from access to excel. 我打开并导出对Excel的查询。 First I created a worksheet in excel and saved it. 首先,我在excel中创建了一个工作表并将其保存。 Then I created a module in the vba part of Access (2013): 然后,我在Access(2013)的vba部分中创建了一个模块:

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 阿罗哈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM