Solved

How to get key of new row after adding?

Posted on 2013-01-12
12
341 Views
Last Modified: 2013-01-12
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
0
Comment
Question by:mlagrange
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
ID: 38770773
If you are recording usernames in your records, add the submitters user id to your DMax criteria.
0
 
LVL 22

Assisted Solution

by:Kelvin Sparks
Kelvin Sparks earned 250 total points
ID: 38770787
Strraight after the .Update you can set a variable that is equal to !PKfieldname. If you do this before you move off the record or close the recordset, you will have the PK of the record you created.

i.e. varPK = rs!PKFieldname


Kelvin
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38770798
Ah... right.   If you are using recordset code,  kelvin's suggestion is hands down the best way to do this.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mlagrange
ID: 38770837
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
0
 

Author Comment

by:mlagrange
ID: 38770839
*Kelvin*  
Sorry...
0
 

Author Comment

by:mlagrange
ID: 38770896
Ok, I added a .MoveLast. That should be "timely" enough?        

        With rstAHdrs
            .AddNew
            ...setting field values...
            .Update
            *.MoveLast*
            lngAHdrID = ![AllocHdrID]
        End With
0
 

Author Closing Comment

by:mlagrange
ID: 38770898
Thanks to you both for your responses
0
 
LVL 75
ID: 38770932
"That should be "timely" enough?"
No.

mx
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38770941
I  believe it has to be immediately before the update.
0
 
LVL 75
ID: 38770947
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.
0
 

Author Comment

by:mlagrange
ID: 38770964
Ok, I'll use that one - Thanks
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38771003
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

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question