UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> Identity    



Contents

Synopsis

Function fIdentity returns the autonumber pk value of the most recently inserted record. The funcion is useful when you need the identity of a new primary record so the you can insert related records into another table.

CODE
' Identity
' http://www.utteraccess.com/wiki/Identity
' Code courtesy of UtterAccess Wiki
' Licensed under Creative Commons License
' http://creativecommons.org/licenses/by-sa/3.0/
'
' You are free to use this code in any application,
' provided this notice is left unchanged.
'
' rev  date                          brief descripton
' 1.0  2010-09-06                    Initial Revision
'
'---------------------------------------------------------------------------------------
' Procedure : fIdentity
' DateTime  : 05/08/2006 11:24
' ' Purpose   : get the identity  (PK) of the most recently inserted record
'---------------------------------------------------------------------------------------
'
Public Function fIdentity() As Long
   Dim rs As DAO.Recordset
   Dim strSQL As String
   Dim lResult As Long

  On Error GoTo fIdentity_Error
 
       strSQL = "SELECT @@IDENTITY as LastID;"
       
       Set rs = CurrentDb.OpenRecordset(strSQL)
       lResult = rs!LastId
       rs.Close
       Set rs = Nothing

   fIdentity = lResult

fIdentity_Exit:

  On Error GoTo 0
  Exit Function

fIdentity_Error:
   Select Case Err.Number

   Case Else
       MsgBox "Error " & Err.Number & " (" & Err.Description & ") " _
           & "in procedure fIdentity "

     
   End Select

End Function

Creative Commons License
Identity by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 5,787 times.  This page was last modified 08:43, 6 April 2011 by Jack Leach. Contributions by Glenn Lloyd  Disclaimers