Updating table with recordSource - Error 3020

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • simaonobrega
    New Member
    • Jul 2017
    • 22

    Updating table with recordSource - Error 3020

    Dear Community,

    I am trying to update all entries from table "tbValve" when a specific criteria is verified (regarding Fabric value).
    When implementing the code bellow, an error 3020 occurs (Update or CancelUpdate without AddNew or Edit).
    Need help because I cannot understand why is this happening.
    Thank you!!

    Code:
            Dim rs As DAO.Recordset, query1 As String
            query1 = "Select TagValve from tbValve Where Fabric = '" & cbEdit.Column(1) & "'"
            Set rs = CurrentDb.OpenRecordset(query1)
            With rs
            If Not .BOF And Not .EOF Then 
            .MoveFirst
            .Edit
            Do While Not rs.EOF
            !TagValve = Replace(cbEdit.Column(0), cbEdit.Column(0), TagValve)
            .MoveNext
            Loop
            .Update
            .Close
            End If
            End With
            Set rs = Nothing
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You are only editing the first row and then updating the last row.

    You have to call edit and update on each row. Move them inside the loop.

    Aside from that, it's probably better to run an update query rather than looping through a recordset.

    Comment

    • simaonobrega
      New Member
      • Jul 2017
      • 22

      #3
      Hello Rabbit,

      Thank you for your answer.
      Even inside the loop, the same error occurs.
      I will try to implement the update query.
      Ty

      Regards

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        It would help to see where you put it in the revised code. The update has to be before the MoveNext. Otherwise you run into the same problem of editing one row but updating a different row.

        Comment

        • simaonobrega
          New Member
          • Jul 2017
          • 22

          #5
          My inattention! You are absolutely right. The problem is now solved with your advice.
          Thank you very much.

          Best regards.

          Comment

          Working...