I've written the following code to update a recordset but when I run it I get a Run-time error 3020: Update or CancelUpdate without AddNew or Edit. When I debug it highlights the rs.update line. I've serached this forum and others for an answer but no luck. Anyone got any ideas? Thanks
[CODE=vb]Option Compare Database
Public Function fImportdata()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordse t("Import Query", dbOpenDynaset)
With rs
.MoveFirst
.Edit
While .EOF = False
If rs![M1] = "Y" And rs![M2] <> "Y" Then
rs![M2] = "Y" ' Will update M2 with a Y if M1=Y and M2 is null.
GoTo MyLabel
Else
End If
If rs![M2] = "Y" And rs![M3] <> "Y" Then
rs![M3] = "Y" 'Will update M3 with a Y if M1=Y and M2 is null.
GoTo MyLabel
Else
End If
If rs![M3] = "Y" And rs![M4] <> "Y" Then
rs![M4] = "Y"
GoTo MyLabel
Else
End If
If rs![M4] = "Y" And rs![M5] <> "Y" Then
rs![M5] = "Y"
GoTo MyLabel
End If
MyLabel:
rs.Update
rs.MoveNext
Wend
End With
rs.Close
End Function[/CODE]
[CODE=vb]Option Compare Database
Public Function fImportdata()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordse t("Import Query", dbOpenDynaset)
With rs
.MoveFirst
.Edit
While .EOF = False
If rs![M1] = "Y" And rs![M2] <> "Y" Then
rs![M2] = "Y" ' Will update M2 with a Y if M1=Y and M2 is null.
GoTo MyLabel
Else
End If
If rs![M2] = "Y" And rs![M3] <> "Y" Then
rs![M3] = "Y" 'Will update M3 with a Y if M1=Y and M2 is null.
GoTo MyLabel
Else
End If
If rs![M3] = "Y" And rs![M4] <> "Y" Then
rs![M4] = "Y"
GoTo MyLabel
Else
End If
If rs![M4] = "Y" And rs![M5] <> "Y" Then
rs![M5] = "Y"
GoTo MyLabel
End If
MyLabel:
rs.Update
rs.MoveNext
Wend
End With
rs.Close
End Function[/CODE]
Comment