Cristen:
There are really two considerations here:
1. Do you want to guarantee sequential numbering when inserting new records?
2. If a record is deleted do you want to reuse its number?
In either case an autonumber doesn't fit the bill. For guaranteeing
sequential values, while an autonumber will mostly do so its not guaranteed
to as its purpose is to guarantee unique values, not sequential values. Not
only that, but if a user starts to insert a new record, but then abandons it,
that number won't be reused (unless the database is first compacted) so gaps
can very easily creep in even without a record being deleted.
Mark has given you a solution to generate sequential numbers, but be aware
that this can be subject to conflicts in a multi-user environment as two or
more users could be inserting a new record simultaneously and each get the
same number. You will of course have uniquely indexed the field, so this
would cause an error in view of the index violation. There are various ways
of avoiding conflicts, one of which you'll find at the following link, which
also allows you to reset the number at which the sequence will restart when
the next record is inserted, provided the number is greater than the highest
already in use (so it doesn't fill gaps)
http://community.netscape.com/n/pfx...yMessages&tsn=1&tid=23839&webtag=ws-msdevapps
A simpler method by Roger Carlson, but without the provision for resetting,
can be found at:
http://www.rogersaccesslibrary.com/...?TID=395&SID=83z7c11zc7b721d2a1e51989c53d7ffb
The second consideration, filling unused gaps after deletions, can be done
if a non-autonumber long integer number data type is used. One way would be
to step through the rows in an ordered recordset based on the table until the
first gap is reached and insert the value into a new record. Another would
be to store the unused numbers in a separate table and get the lowest, by
means of the DMin function, insert it into the new record, and then delete it
from the unused numbers table. However, I'd question the wisdom of this from
a business point of view as once a number has been assigned to an order, if
its the reassigned to another order it could give rise to confusion.
Somebody might have cited the order number in relation to the original order
before deletion in some document outside of the database; to cite it again in
relation to a different order could cause the two orders to be confused.
You'd also need to be absolutely confident that no rows in a related table,
OrderDetails say, contain reference the now deleted order, i.e. referential
integrity and cascade deletions have been enforced, or when you reassign the
number to a new order it will be referenced by the OrderDetails records from
the deleted order. Unless there is an overpowering business reason why you
need an unbroken sequence of numbers it makes a lot more sense to accept that
their values are arbitrary and just allow an autonumber to assign them,
accepting that there may be gaps in the sequence.
Ken Sheridan
Stafford, England