Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 15th, 2006, 10:51 PM
Registered User
Join Date: Oct 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Return Primary Key of Last Append Query

Ok, I'm new to Access and am working with an Access 2000 database. I know that in MS SQL that you can make a stored procedure to insert data into a table and then return the identity of the record inserted (return @@identity).

I would like to create a query to do the same in access. Is there a was I can do this. Currently I have a button that will duplicate a previous order (company name, contact, address, etc...) to populate the fields of a new order. The run another query to select the last item that was inserted into the table. Yet because there are about 6 users who use this table, sometimes when a two people duplicate it will return the wrong order. So then my other way (which seems to work alright) is that the select query select the last order with the specific company information. But I would like to know for future events and it seems the way I am currently completing this task can possibly use a lot of resources down the line.
Old November 16th, 2006, 11:36 AM
Friend of Wrox
Join Date: Sep 2003
Posts: 155
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to leehambly


Totally doable, though doubt you will get much joy by doing it in a query itself. You need to do this in VBA and it is fairly straight forward... I will try to show below some pseudo (though may end up being real) code...

Basically wrap the "add new record" in a function with the pkid being returned by the function, ie:

================================================== ================
Public Function pfunAddRecord(strTableName as string, strValue as string) as long

Dim rst as Recordset

'populates the recordset
Set rst = currentdb.openrecordset(strTableName)

With rst
 .AddNew 'adds a new record
 !Value = strValue 'sets whatever values you have to whatever you need
 pfunAddRecord = !ID 'returns the ID as the function result
 .Update 'can't remember if this is needed or not!
End With

End Function
================================================== =================

I am hoping you understand UDF's, from your take on SQL I assume you do.
Hope it is all good now...

Lee Hambly

Similar Threads
Thread Thread Starter Forum Replies Last Post
Foreign key not updating with Primary key xavier1945 BOOK: Access 2003 VBA Programmer's Reference 2 July 4th, 2007 09:48 PM
Primary key cf2006 BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 1 July 31st, 2006 07:21 PM
FOREIGN KEY and PRIMARY KEY Constraints junemo Oracle 10 June 15th, 2004 01:00 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.