Solved

Restore deleted record with an autonumber field

Posted on 2014-04-08
8
2,862 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
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 34

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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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 34

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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.
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…

747 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

10 Experts available now in Live!

Get 1:1 Help Now