I have a table that after there have been several
deletions, the numbers skip, but in order to perform
certain functions, the autonumber needs to be
consecutive. This primary table is linked to at least 5
other tables. How can I accomplish renumbering all my
records?
With great difficulty! Autonumbers will ALWAYS have gaps; they are
intended for one purpose, and one purpose only - to provide an
almost-guaranteed unique key.
I'd suggest:
- MAKE A BACKUP.
- Test the backup. Make sure it works! You'll be doing open-heart
surgery on this database and it might die on the operating table -
fortunately you can clone it first :-{)
- Create a new Long Integer field in the main table (which will become
the new primary key eventually)
- Create long integer foreign key fields in each of the related tables
- Open the database exclusively so nobody else can be adding records
to mess this up; then run an Update query updating the new field in
the main table to
=DCount("*", "[tablename]", "[AutoID] <= " & [AutoID])
where AutoID is your current autonumber.
- Run Update queries joining the master table to all the related
tables in turn, joining by AutoID to the current foreign key, updating
each related table's new foreign key to the new ID.
- Drop the relationships from the autonumber to its foreign keys, and
establish the new ID as the Primary Key and establish relationships to
the new foreign keys.
- Delete the Autonumber and its corresponding foreign key fields.
- Compact the database.
- Write, or obtain, VBA code to increment the new ID gaplessly. This
is not trivial code! Strictly speaking, it would require that every
higher-numbered record in the table be updated by decrementing it by
one, and every related record as well, if you should ever delete any
record. This would be VERY slow and would cause rapid bloating of your
database.
I must wonder - what functions are you running which cannot tolerate
gaps!?