473,586 Members | 2,472 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieving auto number ID from insert query

Hi

I am using a query to insert a record into a table that has a auto number ID
field. I need to retrieve this ID so I can link the child records with it.
How can I achieve that?

Thanks

Regards
Apr 11 '07 #1
4 15481
Assuming Access 2000 or later, the function below will do it:

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.OpenRecordse t("SELECT @@IDENTITY AS LastID;")
ShowIdentity = rs!LastID
rs.Close

Set rs = Nothing
Set db = Nothing
End Function

Alternatively, you could AddNew to a recordset and get the number.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"John" <Jo**@nospam.in fovis.co.ukwrot e in message
news:OS******** ********@TK2MSF TNGP05.phx.gbl. ..
>
I am using a query to insert a record into a table that has a auto number
ID field. I need to retrieve this ID so I can link the child records with
it. How can I achieve that?
Apr 11 '07 #2
PS: Sorry I am using Access 97. Just read the line that says 'Access 2000 or
later'.

"Allen Browne" <Al*********@Se eSig.Invalidwro te in message
news:46******** *************** @per-qv1-newsreader-01.iinet.net.au ...
Assuming Access 2000 or later, the function below will do it:

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.OpenRecordse t("SELECT @@IDENTITY AS LastID;")
ShowIdentity = rs!LastID
rs.Close

Set rs = Nothing
Set db = Nothing
End Function

Alternatively, you could AddNew to a recordset and get the number.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"John" <Jo**@nospam.in fovis.co.ukwrot e in message
news:OS******** ********@TK2MSF TNGP05.phx.gbl. ..
>>
I am using a query to insert a record into a table that has a auto number
ID field. I need to retrieve this ID so I can link the child records with
it. How can I achieve that?

Apr 11 '07 #3
Hi

Thanks for that.

I get a 'Syntax error : IDENTITY' on the line; Set rs =
db.OpenRecordse t("SELECT @@IDENTITY AS LastID;")

Is @@IDENTITY a reserved word or should I use my id field 'ID' instead?

Thanks

Regards
"Allen Browne" <Al*********@Se eSig.Invalidwro te in message
news:46******** *************** @per-qv1-newsreader-01.iinet.net.au ...
Assuming Access 2000 or later, the function below will do it:

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.OpenRecordse t("SELECT @@IDENTITY AS LastID;")
ShowIdentity = rs!LastID
rs.Close

Set rs = Nothing
Set db = Nothing
End Function

Alternatively, you could AddNew to a recordset and get the number.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"John" <Jo**@nospam.in fovis.co.ukwrot e in message
news:OS******** ********@TK2MSF TNGP05.phx.gbl. ..
>>
I am using a query to insert a record into a table that has a auto number
ID field. I need to retrieve this ID so I can link the child records with
it. How can I achieve that?

Apr 11 '07 #4
Right: That functionality was added in JET 4 (Access 2000), so won't work in
A97.

For Access 97, you will need to use the other approach:

Function ShowID() As Long
Dim rs As DAO.Recordset
Dim lngID As Long
Set rs = dbEngine(0)(0). OpenRecordset(" MyTable", _
dbOpenRecordset , dbAppendOnly)
rs.AddNew
rs![SomeField] = "Some text"
rs![SomeNumber] = 99
rs![SomeDate] = Now()
lngID = rs![YourPrimaryKeyF ieldHere]
rs.Update
rs.Close
ShowID = lngID
End Function

Do replace the names in this one, i.e. MyTable, SomeField, etc.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"John" <Jo**@nospam.in fovis.co.ukwrot e in message
news:LZ******** *************@p ipex.net...
PS: Sorry I am using Access 97. Just read the line that says 'Access 2000
or later'.

"Allen Browne" <Al*********@Se eSig.Invalidwro te in message
news:46******** *************** @per-qv1-newsreader-01.iinet.net.au ...
>Assuming Access 2000 or later, the function below will do it:

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.OpenRecordse t("SELECT @@IDENTITY AS LastID;")
ShowIdentity = rs!LastID
rs.Close

Set rs = Nothing
Set db = Nothing
End Function

Alternativel y, you could AddNew to a recordset and get the number.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"John" <Jo**@nospam.in fovis.co.ukwrot e in message
news:OS******* *********@TK2MS FTNGP05.phx.gbl ...
>>>
I am using a query to insert a record into a table that has a auto
number ID field. I need to retrieve this ID so I can link the child
records with it. How can I achieve that?

Apr 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1565
by: dave | last post by:
I have one auto number field in one table in sql server 2000. I'm inserting new record using insert statement and am not giving any value for that field as its auto number. On the very next statement , I need to know which number it inserted in auto number field for that record?? What should be the logic??? Best Regards, dave
1
2569
by: Ken | last post by:
Need help on the Auto Number or Identity Seed on the Oracle Database I got an Access database that need to be converted to Oracle 9i. Somehow the Trigger we created to simulate the "AUTO NUMBER" on Access could not create the sequence number as soon as the value has been inserted. The sequence number can only be created after we go to the...
5
4534
by: Geoff Cayzer | last post by:
At http://www.blueclaw-db.com/tips_tricks.htm I came across a section which is included below and was hoping for some comment on the article. -------------- Almost never use this auto-number field as the primary key of the table. If you are thinking of hiring an Access programmer or consultant ask
8
6280
by: Carl | last post by:
Hi, I hope someone can share some of their professional advice and help me out with my embarissing problem concerning an Access INSERT query. I have never attempted to create a table with one-to-one relationship but on this occasion I must keep username/password details within a seperate table. Here's the basic specs and database schema:...
4
10237
by: Shahar | last post by:
Hi I need to get a field name 'ID'(that is an auto-number field) right after I add a new row to table, it's work like that: myCommand.ExecuteNonQuery(); myCommand.CommandText = "SELECT @@Identity"; // the auto-number fiels int iId = (int)myCommand.ExecuteScalar();
2
2678
by: kerriejones | last post by:
I am a bit rusty with my php, but I need to move some code on to a new website and coming upon a lot of problems. This one I can not solvefor the life of me. I am trying to insert a record into a table which has an auto number as the key. I was not sure if my connection was the problem so I tried the following select first, but it runs...
3
2168
by: Wayne L | last post by:
Ok now everyone has mentioned not to use auto number if it means anything to the user. My application uses the auto number for exporting only. I append the mastertbl column with my starting number of 0001(4 digits) (prior to populating the table). When information is imported and appended via a query to the mastertbl it will start with 0002 and...
1
2050
by: liorjj | last post by:
Hi All, I'm trying to insert record to Access DB to a tbale that contains its first field as AutoNumber field using the following command: "INSERT INTO Threads VALUES (" + category + "')"; And it gives me the following error: "Number of query values and destination fields are not the same". As far as I'm understand I don't need to...
1
2151
by: Octo Siburian | last post by:
I have been collecting data from ms.access database into a class object '_Get and put in data from database Fingerprint(RAS) into CPresensiFingerprint Public Function GetdbFingerprint() As CPresensiFingerprint Dim presensiFinger As New CPresensiFingerprint Dim SQLSelectdbRAS As String Dim rsFinger As Recordset Dim dateNow_ As Date ...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3838
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1184
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.