1

I have a code looping through a recordset and I'm trying to update the start date from a varible (JS) but its not updating... showing an error saying 'Update or UpdateCancel without addNew or edit'

I have put a .edit just after the ourRecordset![Start Date] = JS which is where its failing

 Dim ourDatabase As Database
    Dim ourRecordset As Recordset
    Dim strSQL As String
    Dim ActvJbNum As Long
    Dim RecCnt As Long
    Dim LpRecCnt As Long
    Dim JS As Date
    
    JS = Now()
    ActvJbNum = Me.EntJobNum
    
    strSQL = "SELECT tblRouting.[Job Number], tblRouting.[Start Date], tblRouting.OpDescription, tblRouting.[Op Seq], tblRouting.OpleadTm " & vbCrLf & _
    "FROM tblRouting " & vbCrLf & _
    "WHERE (tblRouting.[Job Number])= " & EntJobNum & vbCrLf & _
    "ORDER BY tblRouting.[Op Seq] ASC;"
    
    Set ourDatabase = CurrentDb
    Set ourRecordset = ourDatabase.OpenRecordset(strSQL)
    
    
    With ourRecordset
        
        Do Until ourRecordset.EOF
            RecCnt = ourRecordset.RecordCount
            LpRecCnt = LpRecCnt + 1
            ourRecordset![Start Date] = JS
            ourRecordset.Edit
            MsgBox ourRecordset![Start Date] & vbNewLine & ourRecordset![Op Seq] & vbNewLine & LpRecCnt
            
            ourRecordset.MoveNext
            
        Loop
    End With

any idea why it's not updating the start date?

2
  • ourRecordset![Start Date] = JS <-- What are you trying to do here? Why are you trying to write-back to a (read-only) ADODB Recordset object? Are you trying to update values in your database table? If so then you need to execute an explicit UPDATE statement. Commented Sep 30, 2022 at 14:08
  • Hi Dai, thanks for helping. I'm trying to update the tblRouting.[Start Date] field each time it loops trough. At the moment for testing JS is set to Now() but when working the dates will be pulled from the DB. and there's 2 criteria the 1st pass will be different to the rest of the loops. I should be ok with that once i can get it updating Commented Sep 30, 2022 at 14:13

1 Answer 1

1

Call Edit before changing the field value. Afterward, call Update to commit the change.

ourRecordset.Edit
ourRecordset![Start Date] = JS
ourRecordset.Update

However, consider doing this with a single SQL UPDATE, as the commenters recommended, instead of with a recordset. Here it is as a parameterized UPDATE ...

Dim qdf As DAO.QueryDef
Dim strUpdate As String

strUpdate = "UPDATE tblRouting SET [Start Date] = Now() WHERE [Job Number]=[Which Job]"
Set qdf = CurrentDb.CreateQueryDef(vbNullString, strUpdate)
qdf.Parameters("Which Job") = EntJobNum
qdf.Execute dbFailOnError
Sign up to request clarification or add additional context in comments.

2 Comments

As others have said, use a SQL UPDATE statement. It will update all of the rows at the same time and prevent deadlocks.
aghhh.... I was so close...... hrs I've spent trying to work it out, Thanks HandsUp working now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.