5

I've lost my data in Access base, and I've manage to bring them back but when I copy the values in the table with the AutoNumber Column it increments the numbers. Is there Any way to change it to int and then bring it back to AutoNumber?

Pece
  • 637
  • 3
  • 9
  • 20

6 Answers6

10

Here is how I managed to do this in Access 2010:

  1. Make a backup of your database. (Just to be safe.)
  2. Right-click on the table in the tables list, and select Export->Excel. Accept all defaults.
  3. Open the table in Excel and make the desired change to the autonumber field.
  4. Open the table and delete all rows
  5. Right-click on table in the tables list, and select Import->Excel
    • In the options, choose "Append to table" and select the table. Accept defaults for all other options

This might not be a viable solution for a large table. I don't think Excel can handle more than around 65K rows.

Kip
  • 107,154
  • 87
  • 232
  • 265
6

Don't copy the data with the user interface, but append it with a query. Because an Autonumber field is just a long integer with a special default value, you can append to it values that already exist. That doesn't work in the UI, but only in SQL.

An Autonumber field has a few other properties that are different from a normal Long Integer field, but in terms of appending data, those are not relevant. One of those properties is that it is not editable once it's populated, and another is that you can have only one in each table.

David-W-Fenton
  • 22,871
  • 4
  • 45
  • 58
  • Someone please explain the downvote, i.e., why this is an erroneous answer. – David-W-Fenton Jun 29 '10 at 22:01
  • 1
    "Autonumber field is just a long integer with a special default value" = a misstatement. I can add two such 'special default value' columns to the table (only one column may have the Autonumber property). I can use `UPDATE` to change the default value to something else (can't do that to the column with the Autonumber property). The Autonumber column can have a step value other than 1 by creating it using the `IDENTITY(, )` in SQL DLL but can't do the same with a column 'special default value'. Need I go on...? – onedaywhen Jul 06 '10 at 19:52
  • All right. I've changed it. My answer *was* relevant to the issue of appending data, but not complete. – David-W-Fenton Jul 06 '10 at 22:10
  • 1
    SQL code like: ```insert into (, ...) values ( , , ...) ``` It's pretty tedious, but works – charlesdeb May 28 '18 at 03:21
  • 1
    @charlesdeb that SQL was the solution for me. Thanks. It might be worth posting as answer since it's less invasive and easier than a lot of the alternatives. I just popped up a VBA immediate window and ran `DoCmd.RunSQL "INSERT INTO ..."` – Dominic P Jan 30 '19 at 23:34
1

I've manage to insert the AutoNumber fields by code from c#. I take all the data I need and just inserted in an empty table.

Pece
  • 637
  • 3
  • 9
  • 20
0

Make backup of your data table. Delete all data form original table and then do compact & repair your database. By doing this, auto number field will be reset at 1. You may now append your data from backup table.

0

How are you bringing the data back? It should be possible to append the data from your table and to keep the existing numbers.

It is necessary however, that you paste from an integer field to the autonumber field. You cannot change a field to autonumber from integer once there is data in the field, but you can change to integer from autonumber.

Fionnuala
  • 90,370
  • 7
  • 114
  • 152
  • I've bring the data back combining different tables with similar content and writhing the data back to that Table. But when I insert the records the AutoNumber field increments automatically ignoring my entries. – Pece Jun 25 '10 at 08:29
  • Is the data you are brining in an autonumber? – Fionnuala Jun 25 '10 at 08:45
  • Yes, I've managed to make it AutoNumber, but frankly I don't know how because I've done everything same yesterday. Just coping the data in the table, and it managed. – Pece Jun 25 '10 at 13:38
0

SQL code like

   insert into <tablename> 
   (<column 1>, <column2>, ...) 
   values
   ( <value 1>, <value 2>, ...);

will do the trick if you include the autonumber column in your query. It's pretty tedious, but works. You can switch to SQL mode for any old query to enter this text (usually after preparing it in a text editor), or as @Dominic P points out, you can bring up a VBA immediate window and run DoCmd.RunSQL "INSERT INTO ..." which will give you a better editor experience within Access.

charlesdeb
  • 571
  • 5
  • 14