Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset an Access table's AutoNumber field? (it didn't start from 1) [duplicate]

I have a INSERT INTO ... SELECT statement that copies data from one table to another.

The thing though is, the AutoNumber column value in the second table started from the last number in the first one.
Meaning the count of first table is 2000, then, the second table started from 2001.

Using an Access database, how to reset this value?

like image 386
Lucas Juan Avatar asked Dec 23 '13 07:12

Lucas Juan


People also ask

How do I get AutoNumber to start from 100 in access?

For example, if you want the AutoNumber field to start at 100, enter 99 in the Number field. Create and run an append query to append the temporary table to the table whose AutoNumber value you want to change.

How do I set the first value of AutoNumber in Access?

For new tables, AutoNumber begins with 1, and is incremented by 1 with each new row. However, we occasionally want to start the AutoNumber at a number higher than 1. To do this, create an Append query to insert the value of the ID one less than your desired starting number (or Seed value).


2 Answers

You can execute an Access DDL statement from ADO to reset the autonumber seed value. Here is an example Immediate window session:

strDdl = "ALTER TABLE Dummy ALTER COLUMN ID COUNTER(1, 1);"
CurrentProject.Connection.Execute strDdl

The statement must be executed from ADO. It will fail if you try it with DAO (such as CurrentDb.Execute strDdl), or from the Access query designer. The example succeeded because CurrentProject.Connection is an ADO object.

The two values following COUNTER are seed and increment. So if I wanted the autonumber to start from 1000 and increment by 2, I could use COUNTER(1000, 2)

If the table contains data, the seed value must be greater than the maximum stored value. If the table is empty when you execute the statement, that will not be an issue.

like image 83
HansUp Avatar answered Oct 10 '22 03:10

HansUp


Looks like your only option is to move the data into a new table. The following link has some information about how to do it based on your version of access.

Note: be careful if you have relationships to other tables as those would need to be recreated.

http://support.microsoft.com/kb/812718

like image 43
chadkouse Avatar answered Oct 10 '22 02:10

chadkouse