Solved

How to get key of new row after adding?

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

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
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…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…

829 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