Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2013
    Posts
    1

    Arrow Get last ID of primary key automatically generated

    I insert a new record in a table where primary key is automatically generated. I need to get this number (primary key) immediately and send it to client code (MS Dymanics NAV, which uses .NET methods).

    I do know how to manage this issue using MS SQL Server and .NET code (using a stored procedure with an output parameter and @@lastid system parameter to get last ID).



    Do you know how to manage it using MS Access? Maybe using a query object as a stored procedure?

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Here's a couple of methods:
    Code:
    From MVP Ken Snell
    rst.AddNew
        rst.Fields("FieldName1").Value = "YourNewValue1"
        rst.Fields("FieldName2").Value = "YourNewValue2"
    '  (etc. -- do not include the autonumber field as one that
    '   is given a value)
    rst.Update
    rst.Bookmark = rst.LastModified
    lngAuto = rst.Fields("AutonumberFieldName").Value
     
    '---  Another approach
     
    From MVP Allen Browne
    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

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Auto-Generated Email
    By jle0003 in forum Access
    Replies: 3
    Last Post: 08-20-2012, 01:33 PM
  2. Replies: 4
    Last Post: 05-07-2012, 12:08 PM
  3. Automatically generated Copies of Access
    By arunsule in forum Access
    Replies: 4
    Last Post: 08-04-2011, 06:01 AM
  4. Replies: 2
    Last Post: 07-28-2011, 09:20 PM
  5. From which query is the report generated
    By rukmal82 in forum Reports
    Replies: 6
    Last Post: 09-30-2009, 09:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums