3

我想在 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
4

1 回答 1

2

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

例如来自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

例如来自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
于 2013-03-26T11:26:17.130 回答