It depends on what you want to do with this number. Why do you want to show
a sequential number? There are certainly valid cases for doing so, but they
are rare. If this is just a key field you are trying to use to related
parent/child records, then you can use the Autnumber data type and make it
your primary key. This is a value users should never see. It is intended
for the use I described. Another drawback is that Autonumbers do not stay
sequential because of deletions, or canceling after starting a new record,
etc. Also, a Compact/Repair will get you numbering out of sequence. All the
more reason to use Autonumber for primary key fields only.
Now, if there is a really good reason to use a sequential numbering scheme,
it is not that hard to do it.
strNextNum = Format(Nz(DMax("[NumberField]", "MyTable"),0) +1, "0000")
NumberField is the field you carry the list of numbers in - Use your own
field name.
MyTable is where you put the name of the table that has the NumberField
field in it.
the DMax function finds the highest number in NumberField and adds 1 to it.
The Nz function prevents the DMax from returning a Null if there are no
numbers in the database and substitues a 0
The Format function makes sure you number will be 4 digits with leading zeros.
So, what happens when you try to add record 10,000?
microsoft said:
Hi
I am new to access and I not sure if the task I am trying to achieve
requires code or if it is a feature built into access.
I have created a number field in access and I wish to have the field
incremented by one. The starting value of the field should be 1. The
display should however be able to accomodate for digits e.g 0001. Therefore
starting at 0001, I wish to increment to 0002. How can I do this.