无法从Access导出查询到Excel

时间:2016-01-04 20:33:21

标签: excel vba ms-access access-vba

我正在尝试将基于两个组合框的查询导出为ex​​cel。两周前,这段代码有效。我已经移动了数据库,并更新了代码。现在,当我运行它时,它将打开查询,并打开excel,但它不会重写excel文件。它只是打开旧数据。

Access 2010

代码:

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

此处还有打开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

1 个答案:

答案 0 :(得分:0)

原来我保存它的位置与存储数据库的位置不同。保存了正确的文件,而不是在我打开它的位置。轻松修复!程序员错误。