Solved

Restore deleted record with an autonumber field

Posted on 2014-04-08
8
3,099 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 36

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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 36

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
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 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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

830 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