简体   繁体   English

在VBA中以读/写模式从Access导出查询到Excel

[英]To export query from Access to Excel in Read/Write mode in VBA

Below is the code which exports the query named 'LatestSNR' from Access to Excel; 下面是从Access导出名为'LatestSNR'的查询到Excel的代码;

Public Sub Expdata()
    Dim rst As DAO.Recordset
    Dim Apxl As Object
    Dim xlWBk, xlWSh As Object
    Dim PathEx As String
    Dim fld As DAO.Field

    PathEx = Forms("Export").Text14 'path comes from the directory given in form
    Set Apxl = CreateObject("Excel.Application")
    Set rst = CurrentDb.OpenRecordset("LatestSNR")
    Set xlWBk = Apxl.Workbooks.Open(PathEx)
    'xlWBk.ChangeFileAccess xlReadWrite
    Set xlWBk = Workbook("PathEx")
    Apxl.Visible = True

    Set xlWSh = xlWBk.Worksheets("Metadatasheet")

    xlWSh.Activate
    xlWSh.Range("A2").Select

    For Each fld In rst.Fields
        Apxl.ActiveCell = fld.Name
        Apxl.ActiveCell.Offset(0, 1).Select
    Next
    rst.MoveFirst

    xlWSh.Range("A2").CopyFromRecordset rst
    xlWSh.Range("1:1").Select

    ' selects all of the cells
    Apxl.ActiveSheet.Cells.Select

    ' selects the first cell to unselect all cells
    xlWSh.Range("A2").Select
    rst.Close
    Set rst = Nothing

   ' Quit excel
    Apxl.Quit


End Sub

After the execution of code, the query is transferred to excel sheet and is viewed in 'Read only' mode. 执行代码后,查询将转移到Excel工作表,并以“只读”模式查看。 If I try to save it, a copy of the excel file is produced. 如果我尝试保存它,则会生成excel文件的副本。 Can the Excel be opened in Read/Write mode ? 可以在读/写模式下打开Excel吗? so as to save the workbook and also to transfer the query to same workbook repeatedly. 以便保存工作簿,并将查询重复传输到同一工作簿。

Normally, your exported Excel file should not be in read-only mode. 通常,导出的Excel文件不应处于只读模式。 Somewhere in your processing you are not ending the Excel instance properly. 在处理过程中的某个地方,您无法正确结束Excel实例。 A good way to check is in your Task Manager under Processes and see if the EXCEL.EXE remains. 检查的一个好方法是在进程下的任务管理器中查看EXCEL.EXE是否仍然存在。 If so, end the process and make the following code adjustments: 如果是,请结束该过程并进行以下代码调整:

First, properly close and save your Excel file. 首先,正确关闭并保存您的Excel文件。 Quit requires a save or not save confirmation of workbook since it means to Close the Excel application you initialized, not the target workbook. 退出需要保存或不保存工作簿的确认,因为它意味着关闭您初始化的Excel应用程序,而不是目标工作簿。 So add before Apxl.Quit : 所以在Apxl.Quit之前Apxl.Quit

xlWBk.Close True

Second, properly uninitialize your Excel objects to free computer resources. 其次,正确地取消初始化Excel对象以释放计算机资源。 This is not required but simply good coding practice: 这不是必需的,只是良好的编码实践:

Set Apxl = Nothing
Set xlWBk = Nothing
Set xlWSh = Nothing

By the way, unless you do nuanced coding and formatting, there is a built-in Access to Excel function that exports field names and recordset together: DoCmd.TransferSpreadsheet . 顺便说一下,除非你做细微的编码和格式化,否则有一个内置的Access to Excel功能可以将字段名和记录集一起导出: DoCmd.TransferSpreadsheet

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

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