473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Workaround to decrementing Autonumber field

39
Hello Everyone,

I have been searching for ways to prevent the autonumber field from increasing when a user decides he/she does not wish to enter a record.

I searched Bytes and I think I saw something that alluded to setting up two distinct tables (let's call them Table B and Table C). Table B is the one with one field and the field name is the same as the Autonumber field of the host table in question (let's call it Table A). Then Table C is the other field that represents the calculated difference?

I think what happens next is an append query is created to find the difference between the host, and then after that I am lost with how the calculated field passes the next sequential number without any gaps.

If this does not make sense, perhaps someone can provide a more coherent approach.

Can you assist?

Thanks in advance.

Qtip23
May 18 '10 #1
7 3331
TheSmileyCoder
2,322 Expert Mod 2GB
What you can do is to stop using autonumber, and simply use Number, Long type for you field. Then when your about to save the record, you look for the max ID and simply add 1.

I think I would do this, in the forms beforeupdate event:
Expand|Select|Wrap|Line Numbers
  1. If Me.NewRecord Then
  2.   Me.txtID=DMax("ID","myTable")+1
  3. End If
May 18 '10 #2
missinglinq
3,532 Expert 2GB
Smiley's advice is spot on! The very fact that you're concerned about 'missing' numbers shows that you shouldn't be using an Autonumber!

Autonumbers are intended to be used for one purpose and one purpose only, to provide a unique identifier for each record. Here's a post I've archived from a gentleman named John Vinson, MVP, explaining how Autonumbers work and giving even more scenarios where there will be gaps:

When using Autonumber, do be aware that there will be gaps in the numbering - any record that's deleted will leave a gap; hitting <Esc> after starting a record will leave a gap; adding records using an Append query may leave a gap, often a huge one; replicating the database will make your invoice numbers random.

In short... it's best NOT to use Autonumbers for human consumption, and particularly not for consumption by accountants and auditors. Invoice sequences like 319, 321, 322, 385, 386, 221841246, -1083225152 make such people get very nervous.
Linq ;0)>
May 19 '10 #3
Qtip23
39
@TheSmileyOne
Thanks @SmileyONe. it worked like a charm.

I wonder what would make me think I could chnage Autonumber feature...lol @Linq

You both have been very helpful!

Best,
Qtip23
May 19 '10 #4
Qtip23
39
Oh, one more thing. I actually put the code on the form's BeforeInsert event.

I do not want the user to be able to enter any number as I have this field locked on the form. Therefore, the next sequential ID is automatically generated.

Cheers,
Qtip23
May 19 '10 #5
missinglinq
3,532 Expert 2GB
If this is a multi-user app, as it sounds from your post, you need to place the code in the Form_BeforeUpdate event, as Smiley suggested!

When placed there, the ID number will be generated at the very last second before the record is saved, greatly decreasing the chance of two users' records generating the same ID.

Using the Form_BeforeInsert event, on the other hand, generates the ID number the second a single character is entered in a new record, greatly increasing the chances of two records having the same ID number, and popping an error if the ID number is set to No Duplicates, as it should be.

For example, if using the Form_BeforeInsert event, if UserA starts a record, the ID is generated, and before he/she completes the record, UserB starts a record; the same ID will be generated for both.

Linq ;0)>
May 19 '10 #6
Qtip23
39
@Linq duly noted ..just in case we plan to expand for distributed use,, although our team is very small (8 to 10). We plan to have a primary person perform data entry duties. A secondary person will fill in as necessary.

So I guess the form's Record Locks property - Edited Record- would not be capable to avoid the possible record conflict you're describing?

Qtip23
May 20 '10 #7
TheSmileyCoder
2,322 Expert Mod 2GB
The record locks Edited Record, simply means that your current record is now locked, so that others cannot edit the same record at the same time.
If it is a new record it is not even visible to the other users yet, so the lock at this point has no relevance as far as I can see.

Here we are talking about accidentally assigning the same ID to what is intended to be 2 different records.

Is there any reason you are reluctant to put it in the forms BeforeUpdate event?
May 20 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jan Hendrickx | last post by:
Hi all, I use (something like) following code to add new records to a database, but it doesn't work!!! I need to know the value of an AutoNumber-field when I add the record. Why doesn't it...
1
by: Mike Wiseley | last post by:
You can create a query in the SQL view that adds a new column to a table. For example, to add a new text field named X10, you use the following language: Alter Table MyTargetTable1 Add Column...
3
by: ben.werdmuller | last post by:
Hi, Is there an easy way in ASP/VBscript to grab an autonumber (primary key) field just after an SQL insert? This is probably easy, but I'm stuck .. Cheers.
1
by: S. van Beek | last post by:
Dear reader, By append a new record to a table I always expect the next higher value in the range of the autonumber field. But sometimes if I have deleted same records from a table the...
5
by: marko | last post by:
I would like my autonumber field to start at 100000. How can i do that?
6
by: HS1 | last post by:
Hello I have a table in Access Database. This table has a AutoNumber field. I use a DataGrid to show that table When I insert a new record in for this table using a DataGrid, there is a...
9
by: minjie | last post by:
I need to upgrade a MS Access database (Version 2002) with a script only, i.e., via SQL statement, not via Access GUI, and I'm having trouble defining an AutoNumber field. I got an exception error...
1
by: gtwannabe | last post by:
I'm having a problem with a form that uses AutoNumber as the primary key. I have an Abort button to delete the current record and close the form. If AutoNumber is assigned, the code executes a...
1
by: BMF | last post by:
Howdy, I am mainly a MySQL user but I have been playing with Access 07 a little bit recently. I created a database with 3 tables. Table ID --- userID -- AutoNumber UserInfo -...
2
by: Richard Sherratt | last post by:
I've inherited a system that was designed by someone with no understanding of database design. From the logical design point of view, there is no logical design. The physical design is a nightmare....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.