简体   繁体   English

无法将查询从Access导出到Excel

[英]Trouble exporting query to Excel from Access

I am attempting to export a query based on two combo boxes to excel. 我正在尝试导出基于两个组合框的查询以达到卓越。 Two weeks ago, this code worked. 两周前,这段代码奏效了。 I have moved the database, and updated the code. 我已经移动了数据库,并更新了代码。 Now, when I run it, it will open the query, and open excel, but it will NOT rewrite the excel file. 现在,当我运行它时,它将打开查询并打开excel,但不会重写excel文件。 It just opens up the old data. 它只是打开旧数据。

Access 2010 Access 2010

Code: 码:

Private Sub Command14_Click()

DoCmd.OpenQuery "rtnbymonth_qry", acViewNormal, acEdit
DoCmd.OutputTo acOutputQuery, "rtnbymonth_qry", acFormatXLS, "S:\Sales & Use Tax\2016\export.xls"
DoCmd.Close acQuery, "rtnbymonth_qry", acSaveNo

'Open Excel
Call OpenSpecific_xlFile
End Sub

Also here is the code for the program to open excel: 这也是打开excel程序的代码:

'mini program to open excel

Sub OpenSpecific_xlFile()
 '   Late Binding (Needs no reference set)
Dim oXL As Object
Dim oExcel As Object
Dim sFullPath As String
Dim sPath As String


 '   Create a new Excel instance
Set oXL = CreateObject("Excel.Application")


 '   Only XL 97 supports UserControl Property
On Error Resume Next
oXL.UserControl = True
On Error GoTo 0


 '   Full path of excel file to open
On Error GoTo ErrHandle
sFullPath = CurrentProject.Path & "\export.xls"


 '   Open it
With oXL
    .Visible = True
    .Workbooks.Open (sFullPath)
End With


ErrExit:
    Set oXL = Nothing
    Exit Sub

ErrHandle:
    oXL.Visible = False
    MsgBox Err.Description
    GoTo ErrExit
End Sub

Turns out that the location I was saving it to is different than the location where the database is stored. 原来我保存到的位置与数据库存储的位置不同。 The correct file was saved, just not in the location I was opening it. 正确的文件已保存,只是不在我打开它的位置。 Easy fix! 轻松修复! Programmer error. 程序员错误。

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

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