473,776 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
+ 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 2705
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 Relationshipvie w
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
2830
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 work and how can I make it work??? I think it should be easy, but I can't figure it out!
35
7277
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 start at 51 after the last 50 records are deleted? Thanks! Traci
3
7898
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
2420
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 behaviour of the autonumber field is changed.
5
2455
by: marko | last post by:
I would like my autonumber field to start at 100000. How can i do that?
6
6234
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 message that asks me to enter the value for this AutoNumber field. Why I have to do that? Could you please help
9
60101
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 when running the follwoing (in a C++ program using ADO): ...... cmdStr = "CREATE TABLE mytab " "( AutoNumber, " " TEXT(50), " "CONSTRAINT PRIMARY KEY ()); ";
1
3959
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 SQL statement that deletes the current record. I need to be able to detect when AutoNumber is unassigned (a new blank record) so that I can simply close the form without running the SQL delete statement. Unfortunately, no tests I can think of...
2
2049
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. e.g. there are no relationships between tables and foreign keys appear to have been sprinkled at random like confetti. As an example, the Employer - Employee - Contract - Expense hierarchy has EmployerID in the Expense table. That's the...
4
2650
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 autonumber field (the ID field). Is there any way I can make this ID or autonumber field to always begin with 1 so that it acts as a sort of counter/sequential numbering field for the records I put into the table? I am appending the records from a...
0
9628
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9923
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8952
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
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 we have to send another system
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.