Recordset Update Problem

fieldling

Registered User.
Local time
Today, 08:09
Joined
Jul 6, 2007
Messages
36
Sorry if this is long-winded but here goes:

I have the following (example) recordset called "Import Query" that I am trying to update using code:
PHP:
First Name	M1	M2	M3	M4	M5
James		 Y     Y     Y
Peter		 Y				
George		Y     Y     Y     Y

What I need to do is check each record to see which field had the last "Y" and enter a "Y" into the next field. For example James last had a "Y" in M3 so now needs one in M4. Once it is inserted I need to move to the next record and repeat the process (in this case insert a "Y" in M2 for Peter and so on).

I have written the following code but keep getting Run-time Error 3020. Also I think I need to put some code between the End If statements so that once a field is updated it moves on to the next record:

Code:
Public Function fImportdata()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("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.
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.
End If

If rs![M3] = "Y" And rs![M4] <> "Y" Then
rs![M4] = "Y"
End If

If rs![M4] = "Y" And rs![M5] <> "Y" Then
rs![M5] = "Y"
End If
rs.Update

rs.MoveNext
Wend
End With
rs.Close

End Function

Any advice would be much appreciated.

Cheers
 
Sorry if this is long-winded but here goes:

I have the following (example) recordset called "Import Query" that I am trying to update using code:
PHP:
First Name	M1	M2	M3	M4	M5
James		 Y     Y     Y
Peter		 Y				
George		Y     Y     Y     Y

What I need to do is check each record to see which field had the last "Y" and enter a "Y" into the next field. For example James last had a "Y" in M3 so now needs one in M4. Once it is inserted I need to move to the next record and repeat the process (in this case insert a "Y" in M2 for Peter and so on).

I have written the following code but keep getting Run-time Error 3020. Also I think I need to put some code between the End If statements so that once a field is updated it moves on to the next record:

Code:
Public Function fImportdata()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("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.
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.
End If

If rs![M3] = "Y" And rs![M4] <> "Y" Then
rs![M4] = "Y"
End If

If rs![M4] = "Y" And rs![M5] <> "Y" Then
rs![M5] = "Y"
End If

rs.Update

rs.MoveNext
Wend
End With
rs.Close

End Function

Any advice would be much appreciated.

Cheers

I haven't tried this but my inclination would be to
put a statement "myLabel:" on the line before the rs.Update,
put and Goto MyLabel
Else
before each End If

As I said, I haven't tried it.
 
Thanks for the reply. I've updated the code, putting in Else statements as follows, but I'm not sure about the MyLabel statement. Can you explain that to me.:confused:
Code:
Option Compare Database

Public Function fImportdata()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("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.
Else
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.
Else
If rs![M3] = "Y" And rs![M4] <> "Y" Then
rs![M4] = "Y"
Else
If rs![M4] = "Y" And rs![M5] <> "Y" Then
rs![M5] = "Y"
End If
End If
End If
End If
rs.Update
rs.MoveNext
Wend
End With
rs.Close
End Function
 
Thanks for the reply. I've updated the code, putting in Else statements as follows, but I'm not sure about the MyLabel statement. Can you explain that to me.:confused:
Code:
Option Compare Database

Public Function fImportdata()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("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.
Else
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.
Else
If rs![M3] = "Y" And rs![M4] <> "Y" Then
rs![M4] = "Y"
Else
If rs![M4] = "Y" And rs![M5] <> "Y" Then
rs![M5] = "Y"
End If
End If
End If
End If
rs.Update
rs.MoveNext
Wend
End With
rs.Close
End Function

That's not quite what I had in mind. See the attached.
 

Attachments

Ok, I see what you mean, thanks. Anyway I tried it and I still get Error 3020: Update or CancelUpdate without Addnew or Edit. When I debug it highlights the rs.update line. From what I can tell .edit is in the correct place.
 

Users who are viewing this thread

Back
Top Bottom