无法在EXCEL VBA中更新ADO Recordset

时间:2016-06-21 06:30:15

标签: excel excel-vba ado recordset vba

我使用excel范围作为数据源创建记录集。代码是

    Sub Hello()

    Dim xlXML             As Object
    Dim adoRecordset      As Object
    Dim rng               As Range

    Set rng = Range("A1:C6")
    Set adoRecordset = CreateObject("ADODB.Recordset")
    Set xlXML = CreateObject("MSXML2.DOMDocument")
    xlXML.LoadXML rng.Value(xlRangeValueMSPersistXML)
    adoRecordset.CursorLocation = 3
    adoRecordset.Open xlXML, CursorType:=2, LockType:=3

    adoRecordset.Movefirst

    adoRecordset.Fields(1) = 1000 'this is the error line
    adoRecordset.Update

    Set adoRecordset = Nothing
    Set xlXML = Nothing

    End Sub

我无法更新数据集,收到错误"多步操作产生错误。检查每个值(-2147217887)"。我不知道我哪里出错了。我使用Excel 2007.

1 个答案:

答案 0 :(得分:0)

我认为在记录集中添加新行或编辑现有行之前,您必须放置:

adoRecordset.Edit
' OR
adoRecordset.AddNew