无法更新已连接的记录集 - MS Access

时间:2015-01-27 15:49:30

标签: sql vba ms-access ms-access-2013

以下代码给出了一个错误:“无法更新。数据库或对象是只读的。”我有一个隐含的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

2 个答案:

答案 0 :(得分:2)

您可以保持查询相同,只需更改更新即可。 而不是rs.ccc =做类似的事情:

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

DoCmd.RunSQL strSQL 

答案 1 :(得分:1)

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