views:

431

answers:

5

I have a number of child tables that have a foreign key to a parent table.

How do I add an entry in the parent table and get the primary key of that entry, so that I can then enter rows in the child tables that point to the entry in the parent table?

I'm doing this in a MS Access Database from a C# application.

+5  A: 

Microsoft Access 2000 or later does support the @@IDENTITY property to retrieve the value of an Autonumber field after an INSERT. (msdn)

Edit: This is the link to a similar article for .NET 3.5

Instantsoup
-1 for referencing outdated information. The .NET 3.5 section makes not mention of this.
leppie
Leppie, writting the updated information would have been great!!
Added a 3.5 link, which is available on the originally linked page.
Instantsoup
+3  A: 

Try looking into the global variables that will give you the identity value. In SQL Server it is:

SELECT @@identity

Also look into the Scope_Identity() function

Joe Philllips
+1  A: 

Should be able to SELECT @@IDENTITY even though you will have to use a second query to do so. I don't think MS Access will allow it to be combined into one query.

devmode
+4  A: 

Getting the identity of the most recently added record

Pavel Chuchuva
Great link with an excellent code example.
wcm
A: 

Already answered (where the number of referencing tables is one). See:

http://stackoverflow.com/questions/151905/best-way-to-make-double-insert

onedaywhen