Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

sql - Reset autonumber seed

I have a VB6/Access application that occasionally encounters a problem with wrong autonumber field seed.

Lets say there is a table MYTABLE with an autonumber field ID (that is also the primary key). Lets say at the moment the maximum value of ID is 1000. When the application inserts a new record (ID value is not provided explicitly), for some reason it decides that the next autonumber field value is 950 (and not 1001 as it should be) - so a primary key violation error occurs.

I found a KB article that describes my symptoms: http://support.microsoft.com/kb/884185 . In short, they suggest to run a query:

ALTER TABLE MYTABLE ALTER COLUMN ID COUNTER(1001,1)

When I try to do this, it fails with "Invalid field data type"

The problem gets fixed if I open the database in Access and do compact/repair, but I need to be able to fix such problems inside the application: it is installed on a couple of thousands of PCs around the world, and asking people to compact/repair with Access is not an option.

I use DAO DBEngine.CompactDatabase to perform compact/repair inside the application, but it doesn't fix the seed problem, and some additional tricks are needed.

Any ideas for a solution?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please reference the following article, it contains a method you may add to your access project to execute to reset seeding. It has been a saver for me several occasions in the past:

http://allenbrowne.com/ser-40.html

In addition to this it gives explanation and insight into causes and potential resolution for such problems.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...