Link to home
Start Free TrialLog in
Avatar of openaccount
openaccount

asked on

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
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

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.


Avatar of openaccount
openaccount

ASKER

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.
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

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





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.
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
ASKER CERTIFIED SOLUTION
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

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