Re: Is it possible to determine the record ID when using INSERT INTO?
- From: "'69 Camaro" <ForwardZERO_SPAM.To.69Camaro@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 12 Apr 2007 09:00:09 -0700
Hi, Tom.
If you're going to open a recordset anyway, I would use it to insert
as well.
For Access 97, there's only one reliable choice in determining the
AutoNumber of a newly inserted record: AddNew, set the value of each of the
Required columns (and perhaps the rest of the columns), and then Update the
Recordset. Read the value of the AutoNumber column at any time after the
AddNew operation, but before moving to another record or closing the
Recordset.
For Access 2000 and later, that method is going to be slower than first
executing the append query via the Execute procedure and then opening the
Recordset to read the value of the AutoNumber column.
In other words, I wouldn't insert records using a Recordset unless that's my
only choice or I need a slower database application.
HTH.
Gunny
See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
Blogs: www.DataDevilDog.BlogSpot.com, www.DatabaseTips.BlogSpot.com
http://www.Access.QBuilt.com/html/expert_contributors2.html for contact
info.
"Tom van Stiphout" <no.spam.tom7744@xxxxxxx> wrote in message
news:d8ds13h1kgd7gu0a683l917bjsi4apoqsv@xxxxxxxxxx
On Thu, 12 Apr 2007 20:45:03 +0800, "Allen Browne"
<AllenBrowne@xxxxxxxxxxxxxx> wrote:
If you're going to open a recordset anyway, I would use it to insert
as well.
Then rather than just reading the value of the key field, I think you
need to do this:
set rs=db.OpenRecordset("SomeTable", dbOpenTable)
rs.AddNew
...
rs.Update
rs.Move 0, rs.LastModified
ShowIdentity = rs!PK_Field
-Tom.
In Access 2000 and later, you can do this:
Function ShowIdentity() As Variant
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = DBEngine(0)(0)
db.Execute "INSERT INTO MyTable ( MyField ) SELECT 'nuffin' AS Expr1;"
Set rs = db.OpenRecordset("SELECT @@IDENTITY AS LastID;")
ShowIdentity = rs!LastID
rs.Close
Set rs = Nothing
Set db = Nothing
End Function
In any version of Access, you can OpenRecorset, AddNew, and read the value
of the key field.
.
- Follow-Ups:
- Re: Is it possible to determine the record ID when using INSERT INTO?
- From: Tom van Stiphout
- Re: Is it possible to determine the record ID when using INSERT INTO?
- References:
- Is it possible to determine the record ID when using INSERT INTO?
- From: MLH
- Re: Is it possible to determine the record ID when using INSERT INTO?
- From: Allen Browne
- Re: Is it possible to determine the record ID when using INSERT INTO?
- From: Tom van Stiphout
- Is it possible to determine the record ID when using INSERT INTO?
- Prev by Date: Different Startup for Groups of Users - Need 2 mde's?
- Next by Date: Query to remove middle initial
- Previous by thread: Re: Is it possible to determine the record ID when using INSERT INTO?
- Next by thread: Re: Is it possible to determine the record ID when using INSERT INTO?
- Index(es):
Relevant Pages
|