Link to home
Start Free TrialLog in
Avatar of mlagrange
mlagrangeFlag for United States of America

asked on

How to get key of new row after adding?

Hello - when you add a record to an Access (2010) table in VBA, using .AddNew, .Update, and the primary key is an AutoNumber field, is there a dependable way to get that new primary key value?

I am currently using DMax(), but I want to be completely sure I'm getting the record *I* just added, and not that of somebody that slipped in right after my update.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah... right.   If you are using recordset code,  kelvin's suggestion is hands down the best way to do this.
Avatar of mlagrange

ASKER

Kevin - I like that, but I'm getting "no current record" when it hits that line.
Do I have to back up to the previous record or something?
Here's the code (this "NoMatch" is right after a ".Seek"; if NoMatch. I have to add a record, and then add a child record to the newly-added record, so I need this newly-added record key):

If rstAHdrs.NoMatch Then
        With rstAHdrs
            .AddNew
            ...setting field values...
            .Update
            lngAHdrID = ![AllocHdrID]   '-- trying to save off; "AllocHdrID" is the primary key
        End With
    Else
        '-- record exists; get the pkey
        lngAHdrID = rstAHdrs![AllocHdrID]
    End If
*Kelvin*  
Sorry...
Ok, I added a .MoveLast. That should be "timely" enough?        

        With rstAHdrs
            .AddNew
            ...setting field values...
            .Update
            *.MoveLast*
            lngAHdrID = ![AllocHdrID]
        End With
Thanks to you both for your responses
"That should be "timely" enough?"
No.

mx
I  believe it has to be immediately before the update.
No need for MoveLast ...
       
       With rstAHdrs
            .AddNew
            ...setting field values...
            .Update
            .Move 0, .LastModified          
            lngAHdrID = ![AllocHdrID]
        End With

However, if ... you really want to be sure you are getting the last ID you added ... the mbizup's approach with UserName in the first post is the closest.
Ok, I'll use that one - Thanks
Hang on there!  :-)

Did you see my comment at http:#a38770941, and Kelvin's explanation?

<<< If you do this before you move off the record or close the recordset, you will have the PK of the record you created.  >>>

The catch is it has to be while you are still editing your new record.  That means (and a minor correction to his post)  before the .Update statement.  If you do this, you are absolutely, positively guaranteed to get the ID of the record you are creating because the ID you are pulling is directly from the recordset (which no one else is using) and the record you are currently editing.  


Using a test table called Table1 and a field called txtField when adding a new record:

Function AddRecord()
    Dim rs As DAO.Recordset
    
    Set rs = CurrentDb.OpenRecordset("Table1", dbOpenDynaset)
    
    rs.AddNew
    rs!txtFld = "blah"
    AddRecord = rs!ID
    rs.Update
    
End Function

Open in new window


Call it with a messagebox to display the value of the ID added:

msgBox AddRecord

Open in new window