Problem to modify an open ADODB recordset in VBA

Atlas123

New Member
Joined
May 28, 2013
Messages
10
I load the recordset with this qry which is strsql in the vba below

select

' ' as UltimateParent, .....etc etc from.....

Then once the field UltimateParent is in the recordset and it is open I try to modify it like this:

Set Rs1(RsLevel, RsIndex) = New ADODB.Recordset

With Rs1(RsLevel, RsIndex)

.CursorLocation = adUseClient

.CursorType = adUseClient

.LockType = adLockOptimistic

.Open strSQL, ConnACDNV

End With



Do Until Rs1(RsLevel, RsIndex).EOF

Rs1(RsLevel, RsIndex).Fields("UltimateParent").Value = "test"

Rs1(RsLevel, RsIndex).Update

Rs1(RsLevel, RsIndex).MoveNext

Loop

The error comes when I try to set the value of the first record to "test"
The error is: Multiple - step operation generates errors. Check each status value.
How do I make a qry with a hard coded column made on the fly in the select statement and then modify the data in the column with VBA once it is in the open recordset?
 
Hmmm. Well, there's nothing simple about that one. Right off the bat your select query is very complex. It seems you are left joining to a nested query that is right joined back to the same table. Offhand, it doesn't look editable. I guess I'd start there - try some direct tests to see if it can indeed be updated (create a recordset without all the other code involved and just see if you can open the recordset and make an update).
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Code:
[COLOR=#333333]Do Until Rs1(RsLevel, RsIndex).EOF[/COLOR]
[COLOR=#333333]Rs1(RsLevel, RsIndex).Fields("UltimateParent").Value = " a"[/COLOR]
[COLOR=#333333]Rs1(RsLevel, RsIndex).Update[/COLOR]
[COLOR=#333333]Rs1(RsLevel, RsIndex).MoveNext[/COLOR]
[COLOR=#333333]Loop[/COLOR]
[COLOR=#333333]Rs1(RsLevel, RsIndex).MoveFirst
[/COLOR]


It should be
Code:
[B][COLOR=#333333]Rs1(RsLevel, RsIndex).MoveFirst
[/COLOR][/B][COLOR=#333333]
[/COLOR][COLOR=#333333]Do Until Rs1(RsLevel, RsIndex).EOF[/COLOR]
[COLOR=#333333]Rs1(RsLevel, RsIndex).Fields("UltimateParent").Value = " a"[/COLOR]
[COLOR=#333333]Rs1(RsLevel, RsIndex).Update[/COLOR]
[COLOR=#333333]Rs1(RsLevel, RsIndex).MoveNext[/COLOR]
[COLOR=#333333]Loop[/COLOR]
 
Upvote 0
Hi Charles. The RS is already in the first position when the loop starts. I set it back to the first position after the loop because I need it back at the first position later on in the code. According to your suggestion I tested to make sure that the RS was really in the first position before the loop starts and it was and I still got the same error. Hope it is clear now.
 
Upvote 0
If the recordset is open and you can't edit it it is most likely that the recordset is not editable because the query you are using is not editable. That can happen for many reasons - aggregate queries are a common one. Queries that don't have primary keys (or unique keys) are another.

To test for sure, check first for the field value (to make sure the field exists and you are on the record). Then try to change it:
Code:
Do Until Rs1(RsLevel, RsIndex).EOF
    Debug.Print "Recordset Rs1(" & rsLevel & ", " & rsIndex & "): " & Rs1(RsLevel, RsIndex).Value [COLOR="#008000"]'//Check recordset[/COLOR]
    Rs1(RsLevel, RsIndex).Fields("UltimateParent").Value = " a" [COLOR="#008000"]'//Attempt to update[/COLOR]
    Rs1(RsLevel, RsIndex).Update
    Debug.Print "New Value: " & Rs1(RsLevel, RsIndex).Value & vbNewLine
    Rs1(RsLevel, RsIndex).MoveNext
Loop
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top