473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Obtaining Primary Key when Updating with ADO

I'm trying to return the AutoNumber Primary key using ADO and I'm struggling. In DAO, before you update, you can retrieve the autonumber that has been generated. Unfortunately I'm on Jet 3.0 so I can't use the @@IDENTITY statement.
I can move to the end of the recordset after update, but it is a multi user application and I can't be sure that the last one will return to the correct number.
I'm sure that there must be a solution to this - can anyone help?

Many thanks,



Daniel.
Aug 7 '07 #1
6 3977
FishVal
2,653 Expert 2GB
I'm trying to return the AutoNumber Primary key using ADO and I'm struggling. In DAO, before you update, you can retrieve the autonumber that has been generated. Unfortunately I'm on Jet 3.0 so I can't use the @@IDENTITY statement.
I can move to the end of the recordset after update, but it is a multi user application and I can't be sure that the last one will return to the correct number.
I'm sure that there must be a solution to this - can anyone help?

Many thanks,



Daniel.
Hi, Daniel.

In ADO Autonumber field is available immediately after you've invoked Recordset.AddNew method. BTW after you've invoked Recordset.Update method cursor remains on the record, so the field is available as well.

Can't understand your difficulties. :)
Aug 7 '07 #2
I can't either! I've just tried it on 2003 and it works fine. I'll check at work again tomorrow and see what mistake I've made. Thanks for the help.
Aug 7 '07 #3
OK, I've tried it again this morning and it isn't working. This is a little test that I've tried:

Expand|Select|Wrap|Line Numbers
  1. Sub adotest()
  2. Dim cnn As New ADODB.Connection
  3. Dim rst As New ADODB.Recordset
  4. Dim sSQL As String
  5.     cnn.ConnectionString = sADOConn
  6.     cnn.Open
  7.     rst.Open "review", cnn, adOpenDynamic, adLockPessimistic
  8.     rst.AddNew
  9.     Debug.Print "1:" & rst("review_id")
  10.     rst("review_date") = Now()
  11.     rst("category") = "Building"
  12.     rst("review_code") = "ADO1"
  13.     rst("review_description") = "Test"
  14.     rst("review_comments") = "Test"
  15.     rst("review_owner") = "it"
  16.     Debug.Print "2:" & rst("review_id").Value
  17.     rst.Update
  18.     rst.MoveLast
  19.     Debug.Print "3:" & rst("review_id").Value
  20.     rst.Close
  21.     cnn.Close
  22. End Sub
And the output is as follows:
Expand|Select|Wrap|Line Numbers
  1. 1:
  2. 2:
  3. 3:34
My connection string is:
Expand|Select|Wrap|Line Numbers
  1. DRIVER=Microsoft Access Driver (*.mdb);
  2. UID = admin;
  3. UserCommitSync = Yes;
  4. Threads = 3;
  5. SafeTransactions = 0;
  6. PageTimeout = 5;
  7. MaxScanRows = 8;
  8. MaxBufferSize = 2048;
  9. FIL=MS Access;
  10. DriverID=281;
  11. DefaultDir=[folder_name];
  12. DBQ=[database_name]
Any thoughts? When I was playing on Access 2003 last night, I did manage to recreate this using a different connection string. I'm running this in Access 97, ActiveX Data Objects 2.5, Jet 3.0.
Thanks for your help.
Aug 8 '07 #4
Problem solved! I realised it must have been in the connection string, and using
Expand|Select|Wrap|Line Numbers
  1. Provider=Microsoft.Jet.OLEDB.4.0; Data Source=[filename]; User Id=admin; Password=
has fixed it nicely.
I'm assuming that it isn't available in the older version of the Access driver.
Thanks for the help previously; this is now closed.
Aug 8 '07 #5
FishVal
2,653 Expert 2GB
OK, I've tried it again this morning and it isn't working. This is a little test that I've tried:

Expand|Select|Wrap|Line Numbers
  1. Sub adotest()
  2. Dim cnn As New ADODB.Connection
  3. Dim rst As New ADODB.Recordset
  4. Dim sSQL As String
  5.     cnn.ConnectionString = sADOConn
  6.     cnn.Open
  7.     rst.Open "review", cnn, adOpenDynamic, adLockPessimistic
  8.     rst.AddNew
  9.     Debug.Print "1:" & rst("review_id")
  10.     rst("review_date") = Now()
  11.     rst("category") = "Building"
  12.     rst("review_code") = "ADO1"
  13.     rst("review_description") = "Test"
  14.     rst("review_comments") = "Test"
  15.     rst("review_owner") = "it"
  16.     Debug.Print "2:" & rst("review_id").Value
  17.     rst.Update
  18.     rst.MoveLast
  19.     Debug.Print "3:" & rst("review_id").Value
  20.     rst.Close
  21.     cnn.Close
  22. End Sub
And the output is as follows:
Expand|Select|Wrap|Line Numbers
  1. 1:
  2. 2:
  3. 3:34
My connection string is:
Expand|Select|Wrap|Line Numbers
  1. DRIVER=Microsoft Access Driver (*.mdb);
  2. UID = admin;
  3. UserCommitSync = Yes;
  4. Threads = 3;
  5. SafeTransactions = 0;
  6. PageTimeout = 5;
  7. MaxScanRows = 8;
  8. MaxBufferSize = 2048;
  9. FIL=MS Access;
  10. DriverID=281;
  11. DefaultDir=[folder_name];
  12. DBQ=[database_name]
Any thoughts? When I was playing on Access 2003 last night, I did manage to recreate this using a different connection string. I'm running this in Access 97, ActiveX Data Objects 2.5, Jet 3.0.
Thanks for your help.
Hmm. You are right, the code seems ok. Maybe smthng A97 specific.
Suggestions:
  • After Recordset.Update method invoking cursor stays at the new record. Check whether at this point AutoNumber field contains valid value.
  • You can use Recordset.Bookmark property to store record identity.
Aug 8 '07 #6
No worries Fishval, it's all sorted. It was bizarrely the driver, and now everything is working perfectly. There'll be no more DAO for me!
Aug 8 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Andreas | last post by:
Hello list, what about uniqueness of inherited primary keys ? eg you have : create table objects ( id int4, date_created timestamp(0), primary key (id)
5
by: Geoff Cayzer | last post by:
At http://www.blueclaw-db.com/tips_tricks.htm I came across a section which is included below and was hoping for some comment on the article. -------------- Almost never use this auto-number...
1
by: BStorm | last post by:
Does anyone know the best way to update a DataTable row column using specific values for a multipart primary key? For example, updating an OrderAmt column in an OrderDetail table where the...
2
by: Agnes | last post by:
I got a table which without primary key. case 1) I can process insertcommand and daMytable.update..etc case 2) However, if i process updatecommand , As I run daMytable.update .It returns error...
0
by: Phil | last post by:
Environment: XP sp2 VS.Net 2005 (Asp.Net 2.0 - c#) Sql Server 2005 Scenario: -Table (Table1) with two columns: Table1Id <- Primary Key Column1
8
by: shumaker | last post by:
I'm wondering if adding an autonumber primary key will improve the performance of a multiuser access database on a network share. I have a website that lists many tips for improving performance of...
11
by: John Nagle | last post by:
The Python SSL object offers two methods from obtaining the info from an SSL certificate, "server()" and "issuer()". The actual values in the certificate are a series of name/value pairs in ASN.1...
0
by: PeterC | last post by:
We're getting numerous deadlocks in a multi-user system where users are coming in and updating their own data. In our troubleshooting/traces, the deadlocks seem to be coming from lock contention...
1
by: =?Utf-8?B?S2VpdGggRHVQb250?= | last post by:
I have created a setup project for a forms application that I will be deploying in the near future. I have added the primary output to the setup project and all dependencies have been correctly...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.