在excel中运行访问宏

时间:2013-03-26 10:02:34

标签: excel-vba vba excel

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

'~~> 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

1 个答案:

答案 0 :(得分:2)

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

E.g。来自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。来自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