操作必须使用可更新查询/SQL-VBA



我正在尝试创建一个非常动态的宏,它将根据用户的选择更新数据库中的不同表。当然,每张表格都有不同的标题和信息。我的更新有问题(当用户向旧表添加新记录时)。这是代码的一部分,问题是当它到达".update"时,我得到了"Operation must use an Updateable Query"错误。

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

放弃此行:

RecSet.CursorLocation = adUseClient

或者试着这样做:

RecSet.CursorLocation = adUseServer

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

"如果CursorLocation属性设置为adUseClient,则记录集将以只读方式访问,并且无法更新到主机的记录集。"

您在此处连接一个字符串-"SELECT*FROM Customers"&Tabla,但我不知道Tabla在哪里。

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