Getting an Invalid Operation error when code executed from inside an MS Access vba routine

I'm getting an invalid operation message when the following line of code is executed:

                strSQLtext = "UPDATE ComponentMaster SET OrderNumberNoSync = true  WHERE searchnumber='" & strSearchNumber & "' And LineID = " & oRS!LineID
                Set oRS2 = oDB.OpenRecordset(strSQLtext)


When I print strSQLtext in the immediate window, then copy it to a query outside the routine, it runs without issue.
invalid-operation.txt
pcalabriaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fabrice LambertConsultingCommented:
To open a recordset, the query must return records.
But queries such as update, delete, or insert do not return records, ehence the error.

Use the execute method of the database object to execute your query.
Sample code:
Dim strSQLtext As String
strSQLtext = "UPDATE ComponentMaster SET OrderNumberNoSync = true  WHERE searchnumber='" & strSearchNumber & "' And LineID = " & oRS!LineID

Dim db As DAO.Database
Set db = currentDb
db.execute strSQLText, dbFailOnError

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pcalabriaAuthor Commented:
@Fabrice  Thanks and thanks for the explanation.  I'm sure now I'll remember but the code provided still does not work.  I get a message that says "Could not update; record locked".  I suspect this is because I am cycling though a recordset and now trying to change one of the records in that recordset (but not the recordset at the cursor).  Can you help?
PatHartmanCommented:
You may have cut off too much of the code.  Put a stop in the code and print the SQL string to the immediate window.  If you don't see the problem, copy it and paste it into the QBE and try to run it from there.

Based on the error message, chances are excellent that the form that is running this code is bound to the form that is running it and the current record is dirty.
Fabrice LambertConsultingCommented:
Well, this is another issue, not related to your first question.

Close this post and open a new one.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.