无法更新加入的记录集 - MS Access

Unable to update a joined recordset - MS Access

下面的代码给我一个错误:"Cannot update. Database or object is read-only." 我有一个隐含的 JOIN 但删除了它,它仍然无法正常工作。任何帮助,将不胜感激。谢谢!

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim mySQL As String
Dim rsSQL As String

rsSQL = "SELECT tblTable1.aaaID, tblTable1.bbbID, tblTable2.ccc, " & _
        "Sum(tblTable2.ddd) AS Name, tblTable1.eee, tblTable2.fff " & _
        "FROM (tblTable3 INNER JOIN tblTable1 ON tblTable3.aaaID = tblTable1.aaaID) " & _
        "INNER JOIN tblTable2 ON tblTable3.aaaID = tblTable2.aaaID " & _
        "GROUP BY tblTable1.aaaID, tblTable1.bbbID, tblTable2.ccc, tblTable1.eee, tblTable2.fff " & _
        "HAVING (((tblTable1.aaaID)=88) AND ((tblTable1.bbbID)=1) AND ((tblTable2.fff)=True));"

Set db = CurrentDb
Set rs = db.OpenRecordset(rsSQL, dbOpenDynaset)

If Not rs.BOF And Not rs.EOF Then
    rs.MoveFirst
    Do Until rs.EOF
        If rs!ccc = "xxx" Then               'This is a text value
           rs.Edit                           'Here is where the code fails
           rs!eee = eee + Name               '"eee" and "Name" are currency
           rs.Update
        Else
            rs.Edit                          'And here, too
            rs!eee = eee - Name
            rs.Update
        End If
        rs.MoveNext
    Loop
Else
    DoCmd.Close acForm, "frmForm1", acSaveYes
    Exit Sub
End If

使用 GROUP BY 构建的查询必须始终不可更新。你需要改变你的方法。使用 VBA 之外的查询来启动可行的查询(因为这样可以方便地进行测试)。

您可以保持查询不变,只需更改更新即可。 而不是 rs.ccc= 做这样的事情:

strSQL = "UPDATE tblTable2 " _
       & "SET eee= (" & rs.eee + rs.name & ")" _
       & "WHERE PK= " & rs.pk & "

DoCmd.RunSQL strSQL