Re: How To Return Record Number From AddNew
- From: "Bob Barrows [MVP]" <reb01501@xxxxxxxxxxxxxxx>
- Date: Mon, 27 Aug 2007 06:59:49 -0400
Jeff Gaines wrote:
I am sure I used to know how to do this but I can't find it in my
notes :-(
After adding a record to an Access Database (programmatically using
C#) I need to know the record number. The code snippet is:
adCON.Open(strConnection, "", "", 0);
adRS.Open(strQuery, adCON, ADODB.CursorTypeEnum.adOpenKeyset,
ADODB.LockTypeEnum.adLockOptimistic, 0);
if (adRS.EOF)
adRS.AddNew(fList, fValues);
else
adRS.Update(fList, fValues);
Is it possible to know what record number has been allocated by the
Autonumber field? I know it before I call the function when updating a
record but it is freshly allocated by Access for AddNew.
With a "server-side" keyset cursor, the autonumber field should cntain the
newly allocated value after the update resulting from the AddNew occurs. If
it isn't, try
1. issuing an explicit call the the recordset's Update method
2. Using a client-side cursor and issuing a call to the recordset's Requery
method after performing the AddNew
My pesonal recommendation: don't use a recordset for data maintenance.
Instead, use SQL DML (INSERT, UPDATE and DELETE) statements, wither
encapsulated in saved parameter queries (stored procedures) or parameterized
with parameter markers. This will allow you to execute the SELECT @@identity
statement to retrieve the new autonumber.
See these links for more information:
http://databases.aspfaq.com/general/how-do-i-get-the-identity/autonumber-value-for-the-row-i-inserted.html
http://support.microsoft.com/default.aspx/kb/221931
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
.
- Follow-Ups:
- Re: How To Return Record Number From AddNew
- From: Jeff Gaines
- Re: How To Return Record Number From AddNew
- References:
- How To Return Record Number From AddNew
- From: Jeff Gaines
- How To Return Record Number From AddNew
- Prev by Date: How To Return Record Number From AddNew
- Next by Date: Re: How To Return Record Number From AddNew
- Previous by thread: How To Return Record Number From AddNew
- Next by thread: Re: How To Return Record Number From AddNew
- Index(es):
Relevant Pages
|
|