excel vba运行现有的访问查询运行时错误

时间:2015-12-09 17:51:49

标签: excel-vba ms-access-2010 vba excel

需要有关错误的帮助。我试图使用excel VBA运行带参数的现有ACCESS查询。我收到运行时错误' -2147217911(80040e90):自动化错误。设置检查点后,似乎问题出现了代码" rs.Open cmd"。谁能帮我吗?非常感谢。

代码如下:

Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter
Dim rs As ADODB.Recordset

Set con = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")

With con
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .Open "U:\databases\myaccessdatabase.mdb"
End With

With cmd
    .activeconnection = con
    .CommandText = "myquery"
    .CommandType = adCmdStoredProc

    .Parameters.Append cmd.CreateParameter("startdate", adDBDate, adParamInput, , "11/01/2015")

    .Parameters.Append cmd.CreateParameter("startdate", adDBDate, adParamInput, , "11/30/2015")

End With


'run query and paste the result into the excel
Set rs = CreateObject("ADODB.Recordset")
rs.Open cmd
Sheet1.Range("A1").CopyFromRecordset rs

 '  The following is the code for write the log information into the VBA immediate window
    Do Until rs.EOF
       Debug.Print rs.Fields("ID").Value
      rs.MoveNext
    Loop

 ' Close and release the sources from the system

rs.Close
con.Close

Set cmd = Nothing
Set rs = Nothing
Set prm = Nothing
Set con = Nothing

End Sub

0 个答案:

没有答案