Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 2 / January 2008

Tip: Looking for answers? Try searching our database.

Can I retrieve the key of my record?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stapes - 21 Jan 2008 11:59 GMT
Hi

I am creating a record using the following code:

Set qd = db.QueryDefs("Append Purchase Order")
   qd.Parameters("pSUPPLIER").Value = [Forms]![Purchase Orders].Form.
[FK_SUPPLIER_CODE]

   qd.Execute dbFailOnError

The key field in Purchase Orders is PK_PurchaseOrder.

Is there any way I can retrieve the key of the record just created?

Stapes
Allen Browne - 21 Jan 2008 12:06 GMT
Assuming these are Access (JET) tables in Access 2000 or later, try this
kind of thing:

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

   Set rs = Nothing
   Set db = Nothing
End Function

Signature

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.

> Hi
>
[quoted text clipped - 11 lines]
>
> Stapes
Stapes - 21 Jan 2008 14:09 GMT
> Assuming these are Access (JET) tables in Access 2000 or later, try this
> kind of thing:
[quoted text clipped - 36 lines]
>
> - Show quoted text -

Brilliant. That works fine. I tried adding a field and priming it with
a random number. Then used the random number to find the new record
and get it's key. A bit long-winded but it worked. I prefer your
method.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.