【问题标题】:Excel VBA to Run and Export Access QueriesExcel VBA 运行和导出访问查询
【发布时间】:2013-10-30 03:24:13
【问题描述】:

我有以下代码从 Excel 启动 Access 数据库中的一系列查询。当这些查询在 Access 中自行运行时,它们可以正常工作并成功生成正确的文件,但是当我使用单击按钮将宏转换为在 Excel 中运行时,我遇到了一些问题。请参阅下面的代码:

Sub AccessImport()

Dim acApp As Object
Dim MyDatabase As String
Dim question As String

question = MsgBox(Prompt:="Are you sure you want to complete this action?  Running this process is lengthy and could take a couple minutes to complete.", Buttons:=vbYesNo, Title:="Run SOD Matrix")

If question = vbYes Then

MyDatabase = "directory string"
OutputFile = "output string"

    'open the database and apend the combination table to existing
    Set acApp = CreateObject("Access.Application")
        acApp.OpenCurrentDatabase (MyDatabase)
        acApp.Visible = True
        acApp.UserControl = True
        acApp.DoCmd.SetWarnings False
        acApp.DoCmd.OpenQuery "QRYDELETE_PS_ROLE_NAMES", acViewNormal, acEdit
        acApp.DoCmd.OpenQuery "QRYDELETE_PS_ROLE_USER", acViewNormal, acEdit
        acApp.DoCmd.OpenQuery "QRYDELETE_SOD_TBL", acViewNormal, acEdit
        acApp.DoCmd.OpenQuery "QRYAPPEND_PS_ROLE_NAMES", acViewNormal, acEdit
        acApp.DoCmd.OpenQuery "QRYAPPEND_PS_ROLE_USER", acViewNormal, acEdit
        acApp.DoCmd.OpenQuery "QRYAPPEND_SOD_TBL", acViewNormal, acEdit
        'acApp.DoCmd.OpenQuery "QRY_HIGH", acViewNormal, acEdit
        acApp.DoCmd.OutputTo acOutputQuery, "QRY_HIGH", "ExcelWorkbook(*.xlsx)", OutputFile, _
            False, "", , acExportQualityPrint
        acApp.DoCmd.SetWarnings True
        acApp.CloseCurrentDatabase
        acApp.Quit
    Set acApp = Nothing

Else
MsgBox ("Process has been cancelled.")
    Exit Sub
End If
MsgBox ("Process has completed successfully.")
End Sub

对于最后一个用于导出和保存输出的查询,我遇到了一个错误,告诉我Property is not found. 我尝试将DoCmd 更改为TransferSpreadsheetacFormalXLS 的格式类型以防止转换问题,但我仍然无法成功完成。我是否需要将此代码放在模块中而不是将其保留在工作表本身上?想法/帮助?

【问题讨论】:

  • 我不使用访问权限,但我可以确定您正在使用访问权限进行后期绑定。在这种情况下,您是否声明了 ACCESS 常量? Const acOutputQuery As Long = 1Const acExportQualityPrint as Long = 0
  • 我没有,甚至不知道这样做。为什么我需要做这些事情?只是为了我自己的教育......我确实找到了一个成功的解决方法。我决定在 Access 宏中将这些查询串在一起,并使用 acApp.DoCmd.RunMacro 运行它。这成功地完成了我真正想做的一切,所以我采取了不同的方向,但达到了正确的目的。
  • acOutputQueryacExportQualityPrint 是访问常量。 Excel 不会理解它们。将我在上面评论中给出的代码放在代码的最顶部。
  • 他们有什么不同吗?

标签: vba ms-access excel


【解决方案1】:

Siddharth 通过以下声明指出了问题:

acApp.DoCmd.OutputTo acOutputQuery, "QRY_HIGH", "ExcelWorkbook(*.xlsx)", OutputFile, _
    False, "", , acExportQualityPrint

如果不引用 Access 对象库,Excel 将不会知道 Access 常量 acOutputQueryacExportQualityPrint

您最好将Option Explict 添加到模块的声明部分,然后从VB 编辑器的主菜单运行调试->编译。当你这样做时,我怀疑你会发现类似这样的行的问题......

acApp.DoCmd.OpenQuery "QRYDELETE_PS_ROLE_NAMES", acViewNormal, acEdit

Excel 对 Access 常量 acViewNormalacEdit 也一无所知。但是,如果您的意图是执行“操作”查询(INSERTUPDATEDELETE 等),那么最好不要识别这些常量。否则,您将在设计视图中打开这些查询,而不是执行它们。

考虑不同的方法...

Const dbFailOnError As Long = 128
'acApp.DoCmd.SetWarnings False ' leave SetWarnings on!
acApp.CurrentDb.Execute "QRYDELETE_PS_ROLE_NAMES", dbFailOnError
acApp.CurrentDb.Execute "QRYDELETE_PS_ROLE_USER", dbFailOnError

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多