Solved

How to get key of new row after adding?

Posted on 2013-01-12
12
338 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
  • 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
 

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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now