将错误作为运行时错误'-2147467259(800004005)':自动化错误未指定的错误

时间:2019-09-09 08:15:21

标签: excel vba

获取错误作为运行时错误'-2147467259(800004005)': 自动化错误rs.update语句中未指定的错误。

Excel Macro的新功能,并且是第一次。请帮助

下面是使用的代码

Sub dbconnection()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

   Dim rs As New ADODB.Recordset
   Dim sqlstr As String
   Dim strconn As String

   strconn = "Driver={SQL Server};Server=testing;Database=testdb;UID=sa;PWD=s123*"
   cn.Open strconn

   sqlstr = "select * from testing124(nolock)"
   rs.Open sqlstr, strconn, adOpenDynamic, adLockOptimistic
   rs.AddNew
   rs!ID = Cells(2, "a").Value
   rs!remarks = Cells(3, "a").Value
   rs.Update
   rs.Close
   cn.Close
   Set cn = Nothing

1 个答案:

答案 0 :(得分:0)

我确实在C:\ temp \ test.accdb和一个带有一些示例条目的表“ testtable”中创建了一个空的MS Access数据库。下面的代码在引用“ Microsoft ActiveX数据对象2.8库”(工具->参考)后,确实将此示例数据集读入excel工作表:

Option Explicit

Sub dbconnection()

Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

    Dim rs As New ADODB.Recordset
    Dim sqlstr As String
    Dim strconn As String

    strconn = "DSN=MS Access Database;DBQ=C:\temp\test.accdb;"
    cn.Open strconn

    sqlstr = "select * from testtable"
    rs.Open sqlstr, cn

    ActiveSheet.Range("A1").CopyFromRecordset rs

    rs.Close
    cn.Close
    Set cn = Nothing

End Sub