views:

651

answers:

2

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.

I hope somebody has an idea for a solution, I'm really close to desperate

Thanks all

+2  A: 

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.

Jakkwylde
I would never add a reference just to support one function -- late binding would be a much better way to do it. But I'm not sure you get anything more than what you'd get with a DAO compact and the ALTER TABLE seed reset. I can't imagine running that code and not compacting afterwards, anyway.
David-W-Fenton
Thanks! This seems to work perfectly
Incidently
A: 

You may also need to make sure that your database is set up to use ANSI 92 so that COUNTER is recognized as a legitimate data type.

In Access 2007 go to Access Options, Object Designers, SQL Server compatability syntax (ANSI 92) to set this.

AMW
You could execute the ALTER TABLE with ADO in order to avoid having to set SQL 92 mode.
David-W-Fenton