vba 在excel中运行访问宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15634177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:14:53  来源:igfitidea点击:

run access macro within excel

excel-vbavbaexcel

提问by user2172916

I want within excel vba to run an access macro and also copy an access query.
When I run the code below, I get an runtime error 3001 on line cn.DoCmd.RunMacro "runCopyqueryVragenlijsten"

我希望在 excel vba 中运行访问宏并复制访问查询。
当我运行下面的代码时,我在 cn.DoCmd.RunMacro "runCopyqueryVragenlijsten" 行上收到运行时错误 3001

'~~> Select access file Database Questionmark.mdb
Set cn = CreateObject("adodb.connection")
Set rs = CreateObject("adodb.recordset")
cn.Open "provider=microsoft.jet.oledb.4.0;data source=D:\Data\Geschiktheidstesten en OPQ-rapport NMBS competenties\Analyse\Database Questionmark.mdb"

'~~> Run access macro
cn.DoCmd.RunMacro "runCopyqueryVragenlijsten"

'~~> Select access query to copy
rs.Open "select * from Copyquery_Vragenlijsten", cn

'~~> paste access query in sheet vragenlijst
Worksheets("vragenlijst").Range("a2").CopyFromRecordset rs

'~~> Close access file and put cursor on cell A1 of sheet evaluatierooster_NL
rs.Close
Set rs = Nothing
Set cn = Nothing

回答by glh

I suggest running the macro separately then connecting to he file and copying the data.

我建议单独运行宏,然后连接到他的文件并复制数据。

E.g. from http://www.ozgrid.com/forum/showthread.php?t=13173

例如来自http://www.ozgrid.com/forum/showthread.php?t=13173

Sub ExportAccessData()

    Dim mydb As Object

    Set mydb = GetObject("G:\mgai\pertrac\database\StrategyResearch.mdb") 
    mydb.Application.Run "ExportData"  
    mydb.Application.Quit 
    Set mydb = Nothing

End Sub

E.g. from http://support.microsoft.com/kb/131921

例如来自http://support.microsoft.com/kb/131921

Sub Run_Access_Macro()

    Shell("c:\access\msaccess.exe c:\access\sampapps\nwind.mdb")
    Chan = DDEInitiate("MSACCESS", "system") 
    Application.ActivateMicrosoftApp xlMicrosoftAccess
    DDEExecute Chan, "Sample AutoExec"
    Application.DDETerminate Chan

End Sub