我有下面的代码揭开序幕一系列查询在Excel中的Access数据库。当这些查询在Access中自行运行时,它们工作良好并能成功生成正确的文件,但是当我使用按钮单击将宏转换为在Excel中运行时,遇到了一些问题。见下面我的代码:Excel的VBA运行和导出Access查询
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
到TransferSpreadsheet
的的格式类型,以防止转换问题,但我仍然无法成功完成。我是否需要将这些代码放在模块中而不是放在工作表本身上?想法/帮助?
我不接工作,但我可以做出来,你正在访问latebinding。在这种情况下,你是否宣布了ACCESS常量? '常量acOutputQuery只要= 1'和'常量acExportQualityPrint如龙= 0' –
我没有,甚至不知道这样做。为什么我需要做这些事情?只是我自己的教育.... 我没有找到一个工作,周围是成功的。我决定将这些查询串在一起,并使用acApp.DoCmd.RunMacro运行它。这成功地做了我真正想要的一切,所以这是我采取的另一个方向,但取得了正确的结局。 – Nate
'acOutputQuery'和'acExportQualityPrint'是ACCESS常量。 Excel不会理解他们。将我在上面评论中给出的代码放在代码的最上面。 –