473,408 Members | 2,161 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,408 software developers and data experts.

Autonumber the same as existing key

Hello all:

Thank you in advance for your help.

I have a table with an existing key field "Number" into which a number has been manually keyed. I'd like to now create a field called "New_Number" which is an Autonumber field.

Now the problem. I'd like the New_Number field to contain the old Number values for existing records and then continue to add sequentially. If I just add the Autonumber field the values do not match as the physical and logical sequence of the table does not match.

Can some one suggest how I can do this??

Thanks

Dumarest
Nov 27 '07 #1
3 1247
Here's an idea:

1. Create a new table with the same structure.
2. Change the "Number" field in the new table to autonumber.
3. Append all the records from the old to the new table.
4. Rename your tables so the new one uses the original name.
5. Compact the database

Existing records will have the original number, and any new records added should then start with the next higher autonumber. Don't forget that any relationships will need to be re-set.

This should work but haven't tried it myself.
Nov 27 '07 #2
missinglinq
3,532 Expert 2GB
First off, if your field is actually named Number you need to change that; Number is a reserved word, and using it as an object name will cause you problems sooner or later!

Secondly, Autonumbers shouldn't be used in this manner. As John Vinson, an MVP once said, Autonumbers are not fit for human consumption! Many things cause breaks in the sequence of Autonumbers.

You simply need an auto-incrementing number. You can do this extending your current numbers.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2. If Me.NewRecord Then
  3.   If RecordsetClone.RecordCount = 0 Then
  4.    YourNumber = "1"
  5. 'Me.Dirty = False
  6.   Else
  7.    YourNumber = DMax("val([YourNumber])", "YourTable") + 1
  8. Me.Dirty = False
  9. End If
  10. End If
  11. End Sub
If this is a system where you have multiple users working at the same time, you run into the possibility of two or more users starting a record at the same time and hence two or more records could have the same number. To cut the chances of this down to almost zero, I've added the line Me.Dirty = False after the new number is assigned. If this system is only used by one user at a time, you can remove these lines!

Welcome to TheScripts!

Linq ;0)>
Nov 28 '07 #3
I agree that reserved words should never be used for field names, but every new version of Access adds new reserved words that can break your application. Therefore, you need to adopt a best practice as far as naming your fields to be as specific as possible, i.e. instead of "Number" you might use "Order_Number" which avoids conflict with future Access versions, plus has the benefit of being self-documenting.

However, I think that autonumbers have their place, you just need to understand their limitations. In a financial app that might be audited, you should not use autonumbers because as Linq said you can have gaps - nothing riles an auditor more than gaps in a number sequence :-) In that case, I would suggest using a function to create a new record with the next sequential number, and then open the form filtered for that number. The function might look like this (this is old code, I wouldn't write it exactly this way today but it worked):

Expand|Select|Wrap|Line Numbers
  1. Function NextToolID(Customer)
  2.     ' add the next sequential tool number
  3.     Dim db As Database, rst As Recordset, tmp As Long
  4.     Set db = CurrentDb
  5.     Set rst = db.OpenRecordset("tblHyTechToolLog")
  6.     rst.MoveLast
  7.     tmp = 1 + rst!fldHyTechToolCodeID
  8.     rst.AddNew
  9.     rst!fldHyTechToolCodeID = tmp
  10.     rst!fldCustomerID = Customer
  11.     rst.Update
  12.     rst.Close
  13.     Set rst = Nothing
  14.     Set db = Nothing
  15.     NextToolID = tmp
  16. End Function
  17.  
That's one way to handle unique numbering in a multiuser scenario.

However, if you have a batch process then autonumbers are OK. For example, let's say you have a membership database & each year you run an append query to create an invoice for every active member. Using an autonumber for the invoice number saves you some work, and you can safely show that number to the user.
Nov 28 '07 #4

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

Similar topics

12
by: deko | last post by:
Is there a way to reset the AutoNumber sequence? I have several tables that use the AutoNumber field as the Primary Key, and I'd like to somehow do an Import/Export that will make remove the...
33
by: Lee C. | last post by:
I'm finding this to be extremely difficult to set up. I understand that Access won't manage the primary key and the cascade updates for a table. Fine. I tried changing the PK type to number and...
35
by: Traci | last post by:
If I have a table with an autonumber primary key and 100 records and I delete the last 50 records, the next record added would have a primary key of 101. Is there any way to have the primary key...
1
by: Knut B. | last post by:
I have a problem with a database of concerts, where I recently tried to change the existing database with some new rows/categories and some new names on a few of the existing ones. I have an id...
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...
2
by: junkaccount | last post by:
Hello, I currently have a field named QuoteNumber in a table named Quotes. The field is set as autonumber and is used to assign sequential numbers as users enter information in the table through...
11
by: Alan Mailer | last post by:
A project I'm working on is going to use VB6 as a front end. The back end is going to be pre-existing MS Access 2002 database tables which already have records in them *but do not have any...
4
imrosie
by: imrosie | last post by:
Hello All, (newbie)...I'm working on an Ordering app using the typical tables (customers, orders). I have a Search form to find existing customers prior to entering a new order. On the search form...
7
imrosie
by: imrosie | last post by:
Hello all, My Customer table with Autonumbers used as the customer account number. There are two main forms (Order frm, Customer frm); the Customer's account number displays in a bound control on...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.