tags:

views:

52

answers:

4

Hi,

I have an Access database that is corrupted beyond all repair (don't ask me how, it's a client database). However, we can, using code like .NET ODBC, access the data. Since we have a clean copy of the database, we are planning to transfert all data from the corrupted to the new one.

We only have one problem, it's that the database contains autonums and lots of links. When we transfer the data, we want to make sure all autonum ids are exactly the same as they were before. Othewise, we'll just end up with more problems.

Is there a way to do this ?

Thanks

+1  A: 

I believe ACCESS Select into's will keep the autoincrement unchanged. After that, you can continue working in your clean db.

Tobiasopdenbrouw
So, if I understand correctly, I could SELECT INTO my whole tables and all autonumcolumn and rows values will stay the same, hence saving me from my grim predicament ?
David Brunelle
As your situation is a bit unique (given the corruption), why not just test drive on one table and see what happens? (But yes, that's what I'm saying).
Tobiasopdenbrouw
A: 

Not sure if you can disable the AutoNum. You might be able to reset/adjust it if that helps.

http://www.bluemoosetech.com/microsoft-access-functions.php?jid=1&title=Microsoft%20Access%20Autonumber%20Increments

del.ave
+1  A: 

I don't understand about disabling an autonumber. Your new table can accept autonumber values from the old table even if the destination field is also an autonumber. In this example, the id field is an autonumber in both tables:

INSERT INTO tblFoo_new ( id, foo_text, date_assigned, parent_id )
SELECT old.id, old.foo_text, old.date_assigned, old.parent_id
FROM tblFoo AS old;

tblFoo_new is an empty table, and the database was compacted before executing the INSERT INTO statement ... compact "resets" the autonumber counter.

If tblFoo_new was not empty to begin with, execute could have failed. Same if I had deleted rows from the table, but not compacted to reset the counter.

What's the deal with the links you mentioned? How do they fit into this?

HansUp
Here is an example. I got table A, which contains an autonum, and table B, which has one column that is called ID_A, which is basically a foering key to table A. Now, if I insert the whole content of table A in a new version of this table, I want all value in the autonum to remain exactly the same. I cannot change the structure of the tables in any way. What you are saying is that if I empty my new database, which is currently an old copy of the current database and will be emptied, compact and then select into, all value from my autonum column in table A will be OK ?
David Brunelle
to continue with my other comment, I had the feeling I need somehow to temporerally make sure that the value in the autonum column is not increment automatically, but rather use the value I give it, which can't be done when editing the value in the table view of a table.
David Brunelle
Re your first comment: yes, it should work. Table B will "be happy" as long as it can find the same matching values in Table A, right? Re 2nd comment: You can INSERT INTO an autonumber field if the value you're inserting is greater than the current counter. You might even be able to "fill in a gap" in an existing sequence of autonumbers, but I've never tried ...
HansUp
+3  A: 

Many people misunderstand the Jet/ACE Autonumber field. It is really just a long integer field with a default property and a few special properties (such as noneditability, and limited to one per table).

There is nothing at all preventing you from simply inserting the existing values into new empty tables. The Autonumber values will be maintained because when creating the records, you've provided an explicit value, rather than relying on the default value to populate the field.

The only issue would be if referential integrity is defined, in which case you have to do the appends in order such that parent records are inserted before child records, or you remove RI for the insert, and then put it back afterwards.

I'd likely do the latter, because I have code that can copy relationships from one database to another, so I'd just keep as backup template.

Last of all, you need to determine what caused the corruption and remedy that problem. Otherwise, you're just going to have to do this again someday.

David-W-Fenton
thanks for the tip.Unforatunatly, I cannot find the reason for the corruption because I am not in the environment of the client. However, I do know that ther use this databse for a custom app they made which is buggy so I wouldn't be surprised that this is the cause.
David Brunelle
In general, a buggy application won't corrupt a database. Indeed, I'd say that's the least likely cause. It's much more likely a bad hardware/software environment, or deployed wrong. But we don't have enough information about the situation to suggest anything beyond those generalities.
David-W-Fenton