Solved

Auto increment code in an Access Query field

Posted on 2007-11-19
9
8,839 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

752 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