Updating ADO Recordset

I have an ADO Recordset, and i'm trying to edit a record in it. The problem is that when I try to edit a newly added record I receive this error message:

Run-time error'-2147217864 (80040e38)':

The specified row could not be located for updating: Some values may have been changed since it was last read.

However, if I restart my project and then try to edit the record it works.

I'm willing to give 100 points to the person who can tell me how the fix this problem.
IsaacRUAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
Does your table/view have a Primary Key?
If not, ADO can't upate (add) the row correctly...

What is your database system?
0
 
AzraSoundCommented:
try using the .Update method after adding the new record
0
 
wsh2Commented:
Do a recordset.Resynch method after your Addnew. From MSDN:
--------------------------------------
PRB: Oracle Servers Convert Empty Strings to NULL

The information in this article applies to:

ActiveX Data Objects (ADO), versions 1.5, 2.0, 2.01, 2.1, 2.1 SP2, 2.5

SYMPTOMS
The Visual Basic Run-time error 80040e38, which follows, occurs when updating a record containing a variable-length string field previously set to the empty string:

The specified row could not be located for updating; Some values may have been changed since it was last read.

CAUSE
When setting a field to the empty string, Oracle servers automatically convert the empty string to NULL, but the object requesting the edit is not notified. The next time an edit is performed, the empty string is used to identify the record to update, and it is not found because the server now contains a NULL.

RESOLUTION
Two methods to resolve this follow:

Convert empty strings to NULL before editing a recordset object and submitting the update.

-or-

Execute the Recordset object's Resync method after editing.

MORE INFORMATION
The following Visual Basic (ActiveX Data Objects [ADO]) code illustrates the problem. The error occurs at the second Update method call.

Note that although this sample uses the Microsoft OLE DB Provider for Oracle, this behavior also occurs with the Microsoft ODBC Driver for Oracle. It is also possible to see the server's behavior with Oracle's SQL Plus. Issue an INSERT statement involving an empty string (''), and then issue a SELECT statement. The results of the SELECT will show NULL.

Steps to Reproduce Behavior
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset

Set cnn = New ADODB.Connection
cnn.Open "Provider=MSDAORA;Data Source=mydatasource;User ID=myid;Password=mypassword"
cnn.Execute "CREATE TABLE mytable (a NUMBER PRIMARY KEY, b VARCHAR2(10))"
cnn.Execute "INSERT INTO mytable VALUES (100,'value')"

Set rst = New ADODB.Recordset
rst.CursorLocation = adUseClient
rst.Open "SELECT * FROM mytable", cnn, adOpenStatic, adLockOptimistic
rst.MoveFirst
rst(1) = ""
rst.Update
rst(1) = "new"
rst.Update

rst.Close
cnn.Execute "DROP TABLE mytable"
cnn.Close

Set rst = Nothing
Set cnn = Nothing

The simplest work around for this problem is to use the Resync method on the recordset object as illustrated by this code example:
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset

Set cnn = New ADODB.Connection
cnn.Open "Provider=MSDAORA;Data Source=mydatasource;User ID=myid;Password=mypassword"
cnn.Execute "CREATE TABLE mytable (a NUMBER PRIMARY KEY, b VARCHAR2(10))"
cnn.Execute "INSERT INTO mytable VALUES (100,'value')"

Set rst = New ADODB.Recordset
rst.CursorLocation = adUseClient
rst.Open "SELECT * FROM mytable", cnn, adOpenStatic, adLockOptimistic
rst.MoveFirst
rst(1) = ""
rst.Update

rst.Resync

rst(1) = "new"
rst.Update

rst.Close
cnn.Execute "DROP TABLE mytable"
cnn.Close

Set rst = Nothing
Set cnn = Nothing
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
sergedornseifferCommented:
If using a Database different than Access (like SqlSerrver or Oracle) ensure that you a unique key on the Table
0
 
SurojitsealCommented:
I agree with wsh2.
I had the same problem and resync solved it.
0
 
aidan_gillCommented:
i HAD THIS WITH Sybase and the primary key did the job
0
 
sergedornseifferCommented:
Does that mean I get my first points ???

I am brand-new here

0
 
IsaacRUAuthor Commented:
Thanks for all your comments. I'll try each one out and then give out the points

Thanks
0
 
IsaacRUAuthor Commented:
By the way AzraSound. I did call the update method after I added the record. But this did not solve the problem
0
 
IsaacRUAuthor Commented:
angelIII i'm using Access is my database system. Will i still need a primary key?
0
 
AzraSoundCommented:
wsh2's answer solved another question that was the exact same as yours just a few minutes ago...i imagine that is it
0
 
IsaacRUAuthor Commented:
When I tried to use the ADO Recordsets Resync method I got this error:

Run-time error '-2147467259 (80004005)':

Insufficient key column infomation for updating or refreshing.

0
 
AzraSoundCommented:
its a combination of errors produced from
a) what wsh2 said
b) what angelIII said
0
 
IsaacRUAuthor Commented:
Good point AzraSound.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
For your question of Primary Key:
Yes, you need a key!
You should have Primary Keys on every table...
I think you shouldn't need Resync...
0
 
AzraSoundCommented:
well someone just posted a question about getting the exact same error:

The specified row could not be located for updating.Some
values may have been changed since it was last read

and the solution was to use resynch
0
 
IsaacRUAuthor Commented:
I agree with you angelIII cause when i tried resync i got errors. AzraSound what was the title of the question you were talking about?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Hi AzraSound,
I assume that you know ADO :-)
If yes, you certainly run already into the nice thing that ADO raises many errors, but with the same error number.
Even the error text is sometimes the same grrrr
More even, the error text is depending on same language settings, wich makes it difficult to simply analyse the Err.Description....
(developer's) life would be too easy, and our income too low :-)
0
 
AzraSoundCommented:
its now in the "answered section"  the accepted answer was the exact post the wsh2 has on this question...as a matter of fact, upon seeing the question i simply posted a link over to this question for the questioner to reference...i believe the question was named ".Update method and ADO Recordset"
0
 
AzraSoundCommented:
you have an income?  well we're two different entities all together then my friend
0
 
IsaacRUAuthor Commented:
angelIII you were right, when I added a primary key to the table it allowed the updates. Thanks Alot
0
 
IsaacRUAuthor Commented:
Adjusted points from 100 to 110
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
It was a pleasure
0
 
IsaacRUAuthor Commented:
AzraSound, even though your answer was did not work for me, I was impressed how you got that information. Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.