473,473 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Restart from 1 AutoNumber field

759 Contributor
Hello !
I have more related tables:
T_1, T_2 .... T_k, T_k+1 .... T_n
Every table has a primary key defined as AutoNumber.
For k = 2 to n every table has a field defined as LookUp on T_k-1 primary key.
In the test phase I make new records and I remove records so, the primary key (AutoNumber) loose the natural order: (i.e. start at 287 and the next number is 301).

Question:
Is there an easy way (from VBA) to restore the AutoNumber field to start at 1 with step 1 ? Of course, the relation between related fields must remain the same. Hope you understand what I wish to say.

If the answer is NO, maybe you can teach me how to remove and restore the relationships between tables (from VBA)

I use MS OFFICE 2007.

Thank you !
May 3 '11 #1
3 2691
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
I dont know if there is a VBA solution, however:

If the tables are empty (I.e. no records left) then doing a compact and repair operation will reset the autonumber.
May 3 '11 #2
Mihail
759 Contributor
Thank you, TheSmileyCoder.
I don't know that but is a very good solution to send to the users a clean database.

However, this is not a solution for me. I alredy have a lot of records in my database and there are good (useful) records.

To solve my problem I imagine this scenario:
If in T_k the AutoNumber start at value START_k then in T_k+1 I can replace the values in the LookUp field: ActualValue = ActualValue - START_k + 1. This I can done without problems.

Now I need to remove the relationship between T_k and T_k+1 in order to redefine the primary key in T_k as NUMBER (not AUTONUMBER). If the relationship between tables is not removed an error occurs (that happens at manually but I think is the same if I use VBA).

The next step is to insert in T_k a new field, give this field a name and set it as AutoNumber. I do not use yet ALTERTABLE function but I think that this is the solution for this step.

Next step: Set this new field as primary key, remove the old primary key field, rename the new field with the same name as the removed one and restore the relationship between tables.

So, how to (from VBA):

1) Store for later use the relationship between two tables ? Of course with all the properties (in my case all the relationships preserve referential integrity and there are 1 to many), but, for generality, is better to store all the properties of relationship.

2)Remove the relationship between two tables ?

3)Set the relationship between two tables (with the stored properties)?

I am sure you understand that I try to create a mini program that work like in manual mode. I'll try to make it as generally as I could. Something like this:

Expand|Select|Wrap|Line Numbers
  1. Sub RefreshPrimaryKey(T_1,PrimaryField_1,inDataBase_1,T_2,LookUpField_2,inDataBase_2)
  2.      'Code heare
  3. End Sub
May 3 '11 #3
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
You could try taking a look at this link:
Save Relationshipview
I have not tried it myself, but I think it could be relevant for you.
May 3 '11 #4

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...
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...
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...
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....
4
by: Amy Badgett | last post by:
I have figured out that there is no easy way to create an autonumber field in a query, but that there is a way (supposedly) to create an autonumber field in a table or rather, there is already an...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.