Solved

Auto increment code in an Access Query field

Posted on 2007-11-19
9
8,829 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:openaccount
9 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 20311480
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
 

Author Comment

by:openaccount
ID: 20311508
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
 
LVL 14

Expert Comment

by:bluelizard
ID: 20311552
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
 
LVL 77

Expert Comment

by:peter57r
ID: 20311555
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 6

Expert Comment

by:bknouse
ID: 20312270
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
 

Author Comment

by:openaccount
ID: 20317533
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
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
ID: 20318339
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
 

Author Comment

by:openaccount
ID: 20318374
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
 

Author Comment

by:openaccount
ID: 20318383
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sql calculate averages 18 32
Create A query for disabled users 5 16
select over clause 1 15
In or Between 2 0
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now