By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,491 Members | 1,868 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,491 IT Pros & Developers. It's quick & easy.

Restore autonumber field? or use DMAX in append query?

P: 23
Hi,

Iím desperately looking for some helpÖ I maintain a Contacts database which has about 27,000 records. It was initially set up for the field Contact_ID to be an autonumber (also my PK), and this has worked great for a few years now. Earlier this week we noticed that we had some duplicate Contact_IDís (about 160 of them), which I would have thought to be impossible. No idea how that happened, but it seemed to start last week. I needed to correct this problem, obviously, so I changed the autonumber field to be a number field (which I sincerely hope wasnít a huge mistake!) so that I could edit the values and assign unique IDís to each record. I naively thought I could fix the problematic IDís and then change the field back to be an autonumber Ė apparently not.

I then added the DMAX+1 function to my forms so that when someone adds a new contact, they get assigned a Contact_ID. I thought my problem was solved Ė until today when I needed to import a few hundred contacts by doing an append query (and I have a need to do this about once a month). I canít leave my PK empty, so I thought a workaround would be to put a 0 or 1 in the Contact_ID field, and then run a secondary query that would change the value in that field to be the DMAX+1 result Ė similar to what is described in this related article:
http://bytes.com/topic/access/answers/694370-inserting-new-unique-sequence-numbers-without-autoseq
But that doesnít work, either. When I attempt to run the query, I get the error that ďAccess set 0 fields to Null due to type conversion failure and didnít add the records to the table due to key violationsĒ.

Iím really wishing I still had my autonumber field, but Iím not seeing any way to get that back. I know I could copy the table and reimport the records back in to a table with the autonumber field, but that wonít work because I definitely have some skipped IDís (eg, someone started adding a record and decided not to, so the record was just the ID number Ė Iíve periodically deleted these records from the table). I canít assign new ID numbers to each contact because these IDís are tied to mailing lists and other tables.

Could anyone give me advice with this? Either a way to make my append query work with assigning sequential numbers in the Contact_ID field, or a way to recreate an autonumber field and match things up the way they are?

Thanks a million for any recommendations!
Jan 20 '12 #1

✓ answered by dsatino

I'm sure you'll get other responses as well, but here's what I think the easiest thing to do is:

1.Recreate your table without any data. Set your ID field to the autonumber data type.
2. Re-populate the missing ID's from your current table.
3. Write an append query to your new table using your old table as the source. Be sure to sort the ID field in ascending order.

This should get you what you want. Validate that it did, then delete your old table and rename the new one to the old one.

Share this Question
Share on Google+
3 Replies


dsatino
100+
P: 393
I'm sure you'll get other responses as well, but here's what I think the easiest thing to do is:

1.Recreate your table without any data. Set your ID field to the autonumber data type.
2. Re-populate the missing ID's from your current table.
3. Write an append query to your new table using your old table as the source. Be sure to sort the ID field in ascending order.

This should get you what you want. Validate that it did, then delete your old table and rename the new one to the old one.
Jan 20 '12 #2

P: 23
Thank you! I did this and it worked perfectly - solved all my problems. I appreciate the response!
Jan 23 '12 #3

NeoPa
Expert Mod 15k+
P: 31,709
Katie Howard:
I appreciate the response!
Many posts are appreciated. If they can also be considered correct/most helpful responses to the question of the thread then you can show this by selecting them for Best Answer. I believe this one (Post #2) fits the criteria ;-)
Jan 23 '12 #4

Post your reply

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