3

我正在尝试创建一个非常动态的宏,它将根据用户的选择更新数据库中的不同表。当然,每个表都有不同的标题和信息。我在更新时遇到问题(当用户将新记录添加到旧表时)。这是代码的一部分,问题是当它到达“.update”时,我得到“操作必须使用可更新查询”错误。

Dim DBCnn As ADODB.Connection
Dim RecSet As ADODB.Recordset
Dim sQRY As String
Dim FilePath, Titulo, Tabla As String
Dim LastRow, LastColumn, TotalRecords, Id As Long

Set DBCnn = New ADODB.Connection
Set RecSet = New ADODB.Recordset
DBCnn.Mode = adModeReadWrite
DBCnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & FilePath & ";"

sQRY = "SELECT * FROM Customers" & Tabla ' & " WHERE PopID = " & lngid

RecSet.CursorLocation = adUseClient
RecSet.Open _
    Source:=sQRY, _
    ActiveConnection:=DBCnn, _
    CursorType:=adOpenDynaset, _
    LockType:=adLockOptimistic


Do While Range("A" & LastRow).Value <> ""
' repeat until first empty cell in column A
With RecSet
    .AddNew
    .Fields("Id") = Range("A" & LastRow).Value
    .Fields("Name") = Range("B" & LastRow).Text
    .Fields("Age") = Range("C" & LastRow).Value
    .Update '(Here's my error)
End With
LastRow = LastRow + 1
Loop
4

2 回答 2

1

丢弃此行:

RecSet.CursorLocation = adUseClient

或者试试这样:

RecSet.CursorLocation = adUseServer

请参阅MSDN上CursorLocation 属性 (ADO)的备注部分:

“如果CursorLocation属性设置为adUseClient,则记录集将以只读方式访问,并且无法对主机进行记录集更新。”

于 2013-08-07T20:42:50.413 回答
0

您在此处连接一个字符串 - "SELECT * FROM Customers" & Tabla ,但我看不到在哪里提供了 Tabla。

您是否尝试过直接在 Access 中运行查询?
另外,您是否尝试过更改光标类型? http://www.w3schools.com/ado/prop_rs_cursortype.asp

于 2013-08-07T20:27:03.800 回答