Using a sequence number

S

Safwany

in the Table design, when I am using the Autonumber and adding data, if you
just press escape key it will skip the sequence and write the next number,
therefoer I want to know how to use a sequence number without skipping any
number.
 
D

Dale Fye

Autonumber fields should be used as unique values which have no meaning to
your database users, and you should probably never expose them to your users.

If you need a field that your users will see, which you want to increment by
one for each new record, then you you need to use a function to return that
value for you. A simple version of the function would be:

Public function fnNextRecord() as long

fnNextRecord = NZ(DMAX("PO_Num", "PurchaseOrders"), 0) + 1

End function

This would return the next largest number for use in the [PO Num] field. If
a record is subsequently deleted, and no one has created a PO with a higher
number, then this number would be reused.

The caution with using this is that if you have a multi-user application,
then there is a chance that two users will be creating purchase orders at
almost the same time.
Depending on when you call this function in your code, if you don't save the
record immediately after generating the PO_Num, then the other user could
generate a PO with the exact same number. For this reason, I advise you to
either save the record immediately after generating this number, or not
generate the number until the Forms BeforeUpdate event.
--
HTH
Dale

email address is invalid
Please reply to newsgroup only.
 
J

John Spencer (MVP)

Autonumber fields do not behave that way in Access. The autonumber is
generated immediately when you begin to add a record. If you cancel the
record the number is discarded and the next available number will be used the
next time you try to create a record.

Autonumber is designed to give you a unique value for every record that is
created. That is its only real purpose.

If you need sequential numbers, with no gaps, then you need to implement code
to do so.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top