Auto increment code in an Access Query field

Hi,

I have this question regarding autoincrement. I know Access already has an autoincrement option for a number field but the problem is that if I delete a record then when you add another record it will just continue the number after the deleted record.

e.g.

if we add a record and the number is 1 but we decided that record then place another one we want it to still be 1 and not 2. What we want is to convert a normal field to an autonumber field. this is the formula

<previous record> + 1

e.g.

1
next record will be <previous record> + 1 so 1+1 = 2
next record will be <previous record> + 1 so 2+1 = 3
next record will be <previous record> + 1 so 3+1 = 4
etc...

Hope this makes

Thanks
openaccountAsked:
Who is Participating?
 
peter57rConnect With a Mentor Commented:
If you are stroing numbers in a text field then they will be sorted as text not as numbers.

If you want to store them in a text field you must do as you have indicated and add leading zeros to the value.

THen to get the next number you need to get the numeric value using Val() and use the Format() to provifde the leading zeros.  To habdle the empty tsblre sitiuation you use nz() to convert null to "".

me.txtidtextboxname =Format(val(nz(dmax("idfieldname", "tablename") ,""))+1,"0000")
0
 
peter57rCommented:
You cannot use autonumbers this way.
They work the way they work, which is the way you are seeing it.
You should never rely on autonumbers to provide you with an unbroken sequence of records.  All they are for is to provide a unique record id for record identification within the application..  Ideally, the user should never see an autonumber value, and certainly not have to know it or attach any meaning to it.

If you need a guaranteed unbroken sequence this must be done using VBA code to assign the number just before the record is saved.


0
 
openaccountAuthor Commented:
Yes, what I am asking for is a code on how to autonumber. I would like to transform a regular text field to an autonumber using codes.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
bluelizardCommented:
how many users are going to work with the same database at a given time?

just 1 user: whenever the user adds a record, look up the highest sequence number that exists (e.g., DMax) and create the record with 1+this (see attached example).

several users: you need a central "agent" that provides for the sequence numbers, otherwise there might be double numbers.


--bluelizard

dim h as long
h = dmax("id", "mytable")
currentdb.execute "INSERT INTO mytable (id, field2, field3) VALUES " & cstr(h+1) & ", " & fieldvalue2 & ", " & fieldvalue3 & ")"

Open in new window

0
 
peter57rCommented:
Well it depends on the rate at which new records are added and how many users might be doing that at the same time.

But for many applications you can do a simple Dmax() to find the highest number and just add 1.

So in the Form_Beforeupdate event procedure for your data entry form you have your version of:

me.txtidtextboxname = dmax("idfieldname", "tablename") +1





0
 
bknouseCommented:
I would also like to point out the following....

You are assuming the "deleted" record will always be the current maximum ID in the table.  What happens if you currently have 65 records and you decide to delete record 63.  You now have record 62 followed by 64 and 65.  If you use DMax() + 1, you still get 66.  You could try to CHANGE the IDs of records 64 and 65 to 63 and 64 respectively, but this would require a cascade update for related records.  This also means, historical reports would be different from new runs of the reports.

The moral of this story is ... never try to keep an unbroken chain.
0
 
openaccountAuthor Commented:
that would be ok, we will just delete the current one and not in between records. The reason is sometimes we do a test on the database and we will delete that to add another record.

Hi Peter57r, This is correct:

me.txtidtextboxname = dmax("idfieldname", "tablename") +1

but how about when adding a record to an empty table?

it creates an error when this is applied to an empty query.

I think the logic for it is if record is equals to zero the dmax value will be 0  then 0+1 = 1 else execute only : me.txtidtextboxname = dmax("idfieldname", "tablename") +1

but I am not good in vba codes so I do not know how to do that.

I have also tried it and it creates an error on the max value. between 1,2,3,4,5,6,7,8,9,10... 9 is the highest value so I get a record of 10 everytime. here is how access arranged the numbers.

1
10
2
3
4
5
6
7
8
9

is there a way to make the format of the numbers to 0000 so that it will detect 10 as the highest one? then the arrangement will be as follows:

0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0
 
openaccountAuthor Commented:
OK, Thank you peter57r. That works fine. I have also changed the number field to NUMBER instead of TEXT. Hope to here from you again when I encounter another MS Access Problem.

0
 
openaccountAuthor Commented:
By the way, I also have another question regarding Access but I will post it as a different topic so that new points will be rewarder for that question
0
All Courses

From novice to tech pro — start learning today.