PC Review


Reply
Thread Tools Rate Thread

Retrieve value after insert

 
 
Brian
Guest
Posts: n/a
 
      12th Sep 2009
Access 2003
Front end mde
Back end mdb

Other than @@Identity, is there any other way to retrieve the AutoNumber PK
of a newly-inserted record. Outside a multi-user environment, this is
probably just a DMax call, but it seems wise to avoid that approach in a
multi-user environment.

My usual method of running SQL statements via VBA goes like this:

Dim strSQL as String
strSQL = "INSERT INTO MyTable (MyField1) SELECT MyData"
CurrentDb.Execute strSQL, dbFailOnError

Do I have to replace this with code that opens the DAO connection so I can
use the @@IDENTITY operator, or is there some other way? Of course, I could
use another field where I insert a timestamp, assign it to a variable, and
include it in the WHERE clause of the DMAX call, but that also seems like
extra work.
 
Reply With Quote
 
 
 
 
Douglas J. Steele
Guest
Posts: n/a
 
      12th Sep 2009
Are you really just inserting a single field? You could try using

Dim rsCurr As DAO.Recordset
Dim lngNewAutonumber As Long

Set rsCurr = CurrentDb.OpenRecordset("SELECT MyField1 FROM MyTable WHERE
False")
rsCurr.AddNew
rsCurr!MyField1 = MyData
lngNewAutonumber = rsCurr!ID
rsCurr.Update
rsCurr.Close
Set rsCurr = Nothing


That assumes that your Autonumber field is named ID

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)


"Brian" <(E-Mail Removed)> wrote in message
news:49D5126A-9227-4C5F-857E-(E-Mail Removed)...
> Access 2003
> Front end mde
> Back end mdb
>
> Other than @@Identity, is there any other way to retrieve the AutoNumber
> PK
> of a newly-inserted record. Outside a multi-user environment, this is
> probably just a DMax call, but it seems wise to avoid that approach in a
> multi-user environment.
>
> My usual method of running SQL statements via VBA goes like this:
>
> Dim strSQL as String
> strSQL = "INSERT INTO MyTable (MyField1) SELECT MyData"
> CurrentDb.Execute strSQL, dbFailOnError
>
> Do I have to replace this with code that opens the DAO connection so I can
> use the @@IDENTITY operator, or is there some other way? Of course, I
> could
> use another field where I insert a timestamp, assign it to a variable, and
> include it in the WHERE clause of the DMAX call, but that also seems like
> extra work.



 
Reply With Quote
 
Brian
Guest
Posts: n/a
 
      13th Sep 2009
As you suspected, my example was over-simplified. I actually populate several
fields of a header table with one SQL statement, then need to get the
newly-created header PK to insert as the FK into related detail records using
a subsequent SQL statement.

If I read your post correctly, I just need to structure the insert to occur
via a DAO recordset instead of building the SQL query and then executing it.
To do the additional fields, would it look like something like this?

Set rsCurr = CurrentDb.OpenRecordset("SELECT MyField1, MyField2, MyField3,
MyField4 FROM MyTable WHERE
False")
rsCurr.AddNew
rsCurr!MyField1 = MyData1
rsCurr!MyField2 = MyData2
rsCurr!MyField3 = MyData3
rsCurr!MyField4 = MyData4
lngNewAutonumber = rsCurr!ID
rsCurr.Update
rsCurr.Close
Set rsCurr = Nothing

"Douglas J. Steele" wrote:

> Are you really just inserting a single field? You could try using
>
> Dim rsCurr As DAO.Recordset
> Dim lngNewAutonumber As Long
>
> Set rsCurr = CurrentDb.OpenRecordset("SELECT MyField1 FROM MyTable WHERE
> False")
> rsCurr.AddNew
> rsCurr!MyField1 = MyData
> lngNewAutonumber = rsCurr!ID
> rsCurr.Update
> rsCurr.Close
> Set rsCurr = Nothing
>
>
> That assumes that your Autonumber field is named ID
>
> --
> Doug Steele, Microsoft Access MVP
> http://I.Am/DougSteele
> (no private e-mails, please)
>
>
> "Brian" <(E-Mail Removed)> wrote in message
> news:49D5126A-9227-4C5F-857E-(E-Mail Removed)...
> > Access 2003
> > Front end mde
> > Back end mdb
> >
> > Other than @@Identity, is there any other way to retrieve the AutoNumber
> > PK
> > of a newly-inserted record. Outside a multi-user environment, this is
> > probably just a DMax call, but it seems wise to avoid that approach in a
> > multi-user environment.
> >
> > My usual method of running SQL statements via VBA goes like this:
> >
> > Dim strSQL as String
> > strSQL = "INSERT INTO MyTable (MyField1) SELECT MyData"
> > CurrentDb.Execute strSQL, dbFailOnError
> >
> > Do I have to replace this with code that opens the DAO connection so I can
> > use the @@IDENTITY operator, or is there some other way? Of course, I
> > could
> > use another field where I insert a timestamp, assign it to a variable, and
> > include it in the WHERE clause of the DMAX call, but that also seems like
> > extra work.

>
>
>

 
Reply With Quote
 
Jac Tremblay
Guest
Posts: n/a
 
      10th Feb 2010
Hi Douglas,
I think that your select should be:
Set rsCurr = CurrentDb.OpenRecordset("SELECT ID, MyField1 FROM MyTable WHERE
False")
Otherwise, the line:
lngNewAutonumber = rsCurr!ID
will bug.
Thanks for your precious comments.
--
Jac Tremblay


"Douglas J. Steele" wrote:

> Are you really just inserting a single field? You could try using
>
> Dim rsCurr As DAO.Recordset
> Dim lngNewAutonumber As Long
>
> Set rsCurr = CurrentDb.OpenRecordset("SELECT MyField1 FROM MyTable WHERE
> False")
> rsCurr.AddNew
> rsCurr!MyField1 = MyData
> lngNewAutonumber = rsCurr!ID
> rsCurr.Update
> rsCurr.Close
> Set rsCurr = Nothing
>
>
> That assumes that your Autonumber field is named ID
>
> --
> Doug Steele, Microsoft Access MVP
> http://I.Am/DougSteele
> (no private e-mails, please)
>
>
> "Brian" <(E-Mail Removed)> wrote in message
> news:49D5126A-9227-4C5F-857E-(E-Mail Removed)...
> > Access 2003
> > Front end mde
> > Back end mdb
> >
> > Other than @@Identity, is there any other way to retrieve the AutoNumber
> > PK
> > of a newly-inserted record. Outside a multi-user environment, this is
> > probably just a DMax call, but it seems wise to avoid that approach in a
> > multi-user environment.
> >
> > My usual method of running SQL statements via VBA goes like this:
> >
> > Dim strSQL as String
> > strSQL = "INSERT INTO MyTable (MyField1) SELECT MyData"
> > CurrentDb.Execute strSQL, dbFailOnError
> >
> > Do I have to replace this with code that opens the DAO connection so I can
> > use the @@IDENTITY operator, or is there some other way? Of course, I
> > could
> > use another field where I insert a timestamp, assign it to a variable, and
> > include it in the WHERE clause of the DMAX call, but that also seems like
> > extra work.

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieve GUID on/after insert? Patrick Bates Microsoft ADO .NET 15 27th Jun 2006 10:49 PM
Retrieve autonumber ID after INSERT John Smith Microsoft Access 2 26th Jan 2006 03:57 PM
How to retrieve the autonumber after an insert? =?Utf-8?B?UmljaGFyZA==?= Microsoft Access Database Table Design 4 28th Apr 2005 04:26 PM
Re: How can I retrieve the worksheet tab name and insert it into a ce. Frank Kabel Microsoft Excel Misc 0 13th Sep 2004 07:58 PM
Insert Retrieve SP Axi Microsoft Access ADP SQL Server 2 24th Feb 2004 06:41 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:33 PM.