Solved

Restore deleted record with an autonumber field

Posted on 2014-04-08
8
3,243 Views
Last Modified: 2015-01-19
A single record containing an auto-number field was accidentally deleted from Access 2010 table.  I need to replace it.  How do I give the replacement record the original (auto generated) number.
0
Comment
Question by:John Kincaid
[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
8 Comments
 
LVL 22

Accepted Solution

by:
Flyster earned 500 total points
ID: 39986459
You can accomplish this by doing the following:

1. Open your table in design view
2. Change ID type from Autonumber to number
3. Open table and enter new data with deleted number
4. Close table, copy it and paste it structure only
5. Change new table ID field from number to Autonumber
6. Copy original table and paste it "Append Data to Existing Table" to new table
7. Delete old table and rename the new one

Flyster
0
 
LVL 37

Expert Comment

by:PatHartman
ID: 39986512
The easier way is to use an append query.

An Append query is the ONLY way you can supply an autonumber value.  If you have the record in another table, create a query that selects it.  Then change the query to an append query.  Make sure you include the original autonumber value and map it to the autonumber field in the append.

There is no need to modify the table.  

Sample 1:
INSERT INTO tblFacilityStays ( FacilityStayID, ClientID, FacilityID, StartDT, EndDT, Reason, ChangeBy, ChangeDT, CreateDT )
SELECT tblFacilityStays.FacilityStayID, tblFacilityStays.ClientID, tblFacilityStays.FacilityID, tblFacilityStays.StartDT, tblFacilityStays.EndDT, tblFacilityStays.Reason, tblFacilityStays.ChangeBy, tblFacilityStays.ChangeDT, tblFacilityStays.CreateDT
FROM tblFacilityStays;

Sample 2 - with values instead of select:
INSERT INTO tblFacilityStays ( FacilityStayID, ClientID, FacilityID, StartDT, EndDT, Reason, ChangeBy, ChangeDT, CreateDT )
Values (123, 101010, 30202, #4/1/14#, #4/5/14#, "Recover from surgery.", "Pat", #4/8/14#, #4/1/14#;
0
 

Author Closing Comment

by:John Kincaid
ID: 39986529
Just what I needed, thanks!
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!

 
LVL 75
ID: 39986544
You can re-insert the missing AND AutoNumber by using this example:

INSERT INTO tblEmp ( EmpID, EmpName )
SELECT 20 AS AN, "SomeData" AS EmpName;

This examples assumes the missing AutoNumber is 20 - a gap in the AutoNumber sequence.

mx
0
 

Author Comment

by:John Kincaid
ID: 39986568
Flyster's answer was the first and worked.  Pat Hartman and Joe Anderson provided solutions that I appreciate as well.  Thank you all!
0
 
LVL 37

Expert Comment

by:PatHartman
ID: 39986681
It may have worked but I suggest that you use the Append method in the future.  Flyster's method will not work if you have relationships defined so you would need to delete them first, increasing the opportunities to mess up your data.

FYI, SQL Server and other RDBMS' also allow appending rows with existing identity columns.  For other databases though, you may have to turn on a flag at the table level that tells the engine to allow the addition of PK values.  And of course, in all cases, the append would be rejected if the key value duplicated an existing value.

PS - I hope your first step was to back up the database.
0
 

Author Comment

by:John Kincaid
ID: 39986731
Thanks Pat.
0
 
LVL 1

Expert Comment

by:Epsimso
ID: 40558720
One of my clients had a similar problem and the solution proposed by Pat worked perfectly and has the immense advantage of simplicity.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

752 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