PC Review


Reply
Thread Tools Rate Thread

Capture ID of newly inserted row

 
 
akhosravi4703@hotmail.com
Guest
Posts: n/a
 
      26th Oct 2010
I have an access database that I am scaling up to an Oracle database
via ODBC connection. I've run into a problem capturing the ID column
of a newly created record. I need this ID to populate the FK of
dependent related rows in another table that I am generating at the
same time.

I used to use the following code when using just Access in the past

Dim db as database, rst as recordset, x as long
Set db = currentdb
set rst = db.openrecordset("SELECT * FROM tblMyParentTable")
rst.addnew
rst!Field1 = value1
rst!Field2 = value2
x = rst!IdentityField
rst.update

set rst = db.openrecordset("SELECT * FROM tblMyChildTable")
rst.addnew
rst!FK = X
rst!FieldA = valueA
rst.update

Worked fine so long as you set the X variable before the update
command because Access reserves the new auto number before insertion.
However, this doesn't work with Oracle as the sequence is apparently
not generated until after insertion.

I've been searching all night for the answer, but it seems everything
on the web tells you how to do this when using SQLServer with the
@@Identity method, which won’t work here. I've seen only vague answers
to this with Oracle.

Here's the rub. It will not be practical to write any triggers or
stored procedures within Oracle to do this. I don't have privileges to
do that, nor the time to wait for a DBA to do it. Is there some way I
can do this without cheese balling it using some flavor of max of
identity column?
 
Reply With Quote
 
 
 
 
bala
Guest
Posts: n/a
 
      26th Oct 2010
On Oct 26, 3:01*am, akhosravi4...@hotmail.com wrote:
> I have an access database that I am scaling up to an Oracle database
> via ODBC connection. I've run into a problem capturing the ID column
> of a newly created record. I need this ID to populate the FK of
> dependent related rows in another table that I am generating at the
> same time.
>
> I used to use the following code when using just Access in the past
>
> Dim db as database, rst as recordset, x as long
> Set db = currentdb
> set rst = db.openrecordset("SELECT * FROM tblMyParentTable")
> rst.addnew
> rst!Field1 = value1
> rst!Field2 = value2
> x = rst!IdentityField
> rst.update
>
> set rst = db.openrecordset("SELECT * FROM tblMyChildTable")
> rst.addnew
> rst!FK = X
> rst!FieldA = valueA
> rst.update
>
> Worked fine so long as you set the X variable before the update
> command because Access reserves the new auto number before insertion.
> However, this doesn't work with Oracle as the sequence is apparently
> not generated until after insertion.
>
> I've been searching all night for the answer, but it seems everything
> on the web tells you how to do this when using SQLServer with the
> @@Identity method, which won’t work here. I've seen only vague answers
> to this with Oracle.
>
> Here's the rub. It will not be practical to write any triggers or
> stored procedures within Oracle to do this. I don't have privileges to
> do that, nor the time to wait for a DBA to do it. Is there some way I
> can do this without cheese balling it using some flavor of max of
> identity column?


*************************************************************

Save the record through Code. On the click of the Save Button first
save the Parent record. You will have the ID (Primary Key) of the
Parent Record. Now save the detail records including the foreign key
(Primary key of Parent record)
 
Reply With Quote
 
akhosravi4703@hotmail.com
Guest
Posts: n/a
 
      27th Oct 2010
On Oct 26, 1:46*pm, bala <balk...@gmail.com> wrote:
> On Oct 26, 3:01*am, akhosravi4...@hotmail.com wrote:
>
>
>
>
>
> > I have an access database that I am scaling up to an Oracle database
> > via ODBC connection. I've run into a problem capturing the ID column
> > of a newly created record. I need this ID to populate the FK of
> > dependent related rows in another table that I am generating at the
> > same time.

>
> > I used to use the following code when using just Access in the past

>
> > Dim db as database, rst as recordset, x as long
> > Set db = currentdb
> > set rst = db.openrecordset("SELECT * FROM tblMyParentTable")
> > rst.addnew
> > rst!Field1 = value1
> > rst!Field2 = value2
> > x = rst!IdentityField
> > rst.update

>
> > set rst = db.openrecordset("SELECT * FROM tblMyChildTable")
> > rst.addnew
> > rst!FK = X
> > rst!FieldA = valueA
> > rst.update

>
> > Worked fine so long as you set the X variable before the update
> > command because Access reserves the new auto number before insertion.
> > However, this doesn't work with Oracle as the sequence is apparently
> > not generated until after insertion.

>
> > I've been searching all night for the answer, but it seems everything
> > on the web tells you how to do this when using SQLServer with the
> > @@Identity method, which won’t work here. I've seen only vague answers
> > to this with Oracle.

>
> > Here's the rub. It will not be practical to write any triggers or
> > stored procedures within Oracle to do this. I don't have privileges to
> > do that, nor the time to wait for a DBA to do it. Is there some way I
> > can do this without cheese balling it using some flavor of max of
> > identity column?

>
> *************************************************************
>
> Save the record through Code. On the click of the Save Button first
> save the Parent record. You will have the ID (Primary Key) of the
> Parent Record. Now save the detail records including the foreign key
> (Primary key of Parent record)- Hide quoted text -
>
> - Show quoted text -


You're losing me on the "You will have the ID (Primary Key)" part?
From what I can tell, any new record added must be fully committed and
the datasource requeried before it will yield up what the newly
generated pk was. Even the .LastModified method won't work. Oh, the
record is added to the recordset, but if you navigate to that newly
added record in the recordset and try to extract what the PK is,
you'll get a runtime error that the record was deleted (well, not
deleted, but not completely there either).

At this point I can only see two possibilities. Either I have to write
a stored procedure to create these two records and call it via a
passthrough or I need to write the parent record, run a completely new
search to uniquely identify what that new record was to capture its
PK, then add the child record. I was trying to avoid that middle step
as I view that middle query as wasteful - the database is already slow
enough as it is. Any other thoughts on this?
 
Reply With Quote
 
Access Developer
Guest
Posts: n/a
 
      27th Oct 2010
We used a table of "next unique ids" for the tables we updated with some
server DBs (though not Oracle) along with a stored procedure accessed via a
passthrough query. It would retrieve and update the "next unique id"
recorded in the table, and return it, rather than using the server
equivalent of AutoNumber. Thus, whenever we added the new record, we
already had its unique id.

--
Larry Linson, Microsoft Office Access MVP
Co-author: "Microsoft Access Small Business Solutions", published by Wiley
Access newsgroup support is alive and well in USENET
comp.databases.ms-access


 
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
apply next number in newly inserted row KUMPFfrog Microsoft Excel Programming 2 25th Jan 2009 05:36 PM
Need Help on populating a newly inserted row eainmetlae@gmail.com Microsoft Excel Programming 3 5th Aug 2008 05:53 PM
Getting the key ID from a newly inserted record =?Utf-8?B?YmthdWZtYW4=?= Microsoft Access VBA Modules 8 18th Jan 2008 07:43 PM
Carrying over formulas into newly inserted row =?Utf-8?B?UmlwaGF5?= Microsoft Excel Misc 2 8th Aug 2007 03:02 AM
Calculation does not consider newly inserted data birgit Microsoft Excel Misc 1 9th Jun 2004 03:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:57 PM.