Results 1 to 2 of 2
  1. #1
    rukie is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Posts
    3

    Return Row Identity after Insert Fails

    Hi guys,
    I'm writing a database in access 2007 but I'm trying to maintain its ability to be moved from access/sharepoint/sql server/etc. Currently I need to return the row ID of a field I've just inserted. However, each time I run SELECT @@IDENTITY it returns a value of 0. I'd really appreciate any thoughts on this. I understand its a bad idea to use max when you have multiple users. Scope_Identity() isn't a part of the access db.. Please help lol

    First I call a public function in a module to open the connection:


    [QUOTE][Public Function cnADOAXS()
    Public cnAXS As New ADODB.Connection
    Public rsAXS As New ADODB.Recordset
    Public cmdAXS As New Command

    Public Function cnADOAXS
    Set cnAXS = CurrentProject.Connection
    Set rsAXS = New ADODB.Recordset
    cmdAXS.ActiveConnection = cnAXS
    end Function
    End Function/QUOTE]

    then I execute the following:
    ' Lets create the issue first:
    strTitle = Me.txtTitle.Value
    strDesc = Me.txtDescription.Value
    strImportance = Me.cmbImportance.Value
    strSQLIssue = "INSERT INTO tblIssue ([Title], [Description], [Importance], [CIR]) " & _
    "VALUES ('" & strTitle & "', '" & strDesc & "', '" & strImportance & "', '" & CIRExist & "');"
    ' Lets insert the issue
    Debug.Print strSQLIssue
    Call cnADOAXS
    Set rsAXS = cnAXS.Execute(strSQLIssue, lngRecs)

    Debug.Print lngRecs
    ' Now lets retrieve the ID for that row
    Set rsAXS = cnAXS.Execute("SELECT @@IDENTITY")
    'If Not rsAXS.EOF Then
    ' Display the drawing description in the form
    MsgBox (rsAXS.Fields(0).Value)
    'End If
    Debug.Print rsAXS.Fields(0).Value
    rsAXS.Close
    cnAXS.Close

  2. #2
    pkstormy's Avatar
    pkstormy is offline Access/SQL Server Expert
    Windows XP Access 2003
    Join Date
    Mar 2010
    Location
    Madison
    Posts
    682
    When I run a function/code which updates a recordset and I want to grab the 'newly' created autonumber value, I'll use code like this:

    ...
    rs!SomeField = 20
    rs.update
    dim RID as variant
    RID = rs!MyAutonumberField 'RID will now equal the lastest update autonumber value
    rs.close
    set rs = nothing
    .....

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

Similar Threads

  1. INSERT query: insert new data only
    By drh in forum Access
    Replies: 2
    Last Post: 04-04-2014, 05:31 PM
  2. If insert query fails, then
    By redbull in forum Queries
    Replies: 3
    Last Post: 07-23-2012, 10:08 AM
  3. Calculate age using a persons identity number
    By rwcozens in forum Access
    Replies: 13
    Last Post: 05-03-2012, 06:45 AM
  4. Populating identity field of an over-ODBC linked MSSQL table
    By Passiday in forum Import/Export Data
    Replies: 0
    Last Post: 11-16-2011, 11:49 AM
  5. Replies: 3
    Last Post: 06-02-2009, 09:51 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