How to reset values of auto number field - vba

I have a table in MS access having more than 30 thousand rows.But in this table some values are duplicate so i delete duplicate rows and during this process autonumber fields also delete.Now i want to reset existing autonumber values using any query or vba code.
I use below mentioned query for reset values but query starts next value from one not resetting existing values.
ALTER TABLE [urlcheck] ALTER COLUMN [ID] COUNTER(1,1);
Currently auto number field values are just like this 1,2,5,7,9 now i want to reset values in this format 1,2,3,4,5

Related

Setting Value of One Column same as another column in a table during insert

I have a table Line_Production_Plan in SQL Server; it has a UID column (int, auto-increment, identity).
It also has multiple other columns. One of them is execution_priority (int, not null).
When I insert a new row into the table (via a stored procedure (without passing execution_priority or UID as parameters)), I want execution_priority to take up the same value as the corresponding UID column in during insert. Is there a way to set the default value of a column, equal to another upon insert?
The execution, priority needs to be changed from time to time. Hence I can't use identity or auto increment.
You should use IDENT_CURRENT to get the last identity value generated for the table Line_Production_Plan.
Try this code:
INSERT INTO Line_Production_Plan (execution_priority)
VALUES ((SELECT IDENT_CURRENT('Line_Production_Plan')));

SQL Server Add Default datetime column for existing table

I want to have a new column in the table that will show the date and time of the inserts, but without modifying the queries to include the column itself.
I have added the new column in the following way:
ALTER TABLE DBO.HOURLYMODULETIMES
ADD CreateTime datetime DEFAULT NOT NULL getdate()
This adds the values to previous entries, but when I try to INSERT INTO the table without including the new column
INSERT INTO DBO.HOURLYMODULETIMES VAlUES
(99999999,11111,2222,'JA')
Table has 5 columns ID, AVGMODULETIME, SUMHOURS, USERNAME, CreateTime(newly added). I get the following error:
Column name or number of supplied values does not match table definition.
Is it possible to create such a column without modifying the queries?
You have to specify the columns now when you want to omit one of them when doing INSERT:
INSERT INTO DBO.HOURLYMODULETIMES (ID, AVGMODULETIME, SUMHOURS, USERNAME)
VALUES (99999999,11111,2222,'TEST')
It's good programming practice to always do this, since table definitions may change over time - as you have noticed!

Auto increment Number not working in Access 2007

I have tried to insert data with simple insert statement but it is not increasing auto increment field ID.
My Query is
Insert into [Product]([Name],[Category]) values('abc','xyz')
and my table structure is:
Mstr_Product Id number AutoNumber Name Text Category Text
I have found solution
Insert into Product select 'abc','xyz'
It will automatically increase auto ID

Can't INSERT INTO SELECT into a table with identity column

In SQL server, I'm using a table variable and when done manipulating it I want to insert its values into a real table that has an identity column which is also the PK.
The table variable I'm making has two columns; the physical table has four, the first of which is the identity column, an integer IK. The data types for the columns I want to insert are the same as the target columns' data types.
INSERT INTO [dbo].[Message] ([Name], [Type])
SELECT DISTINCT [Code],[MessageType]
FROM #TempTableVariable
This fails with:
Cannot insert duplicate key row in object 'dbo.Message' with unique index
'IX_Message_Id'. The duplicate key value is (ApplicationSelection).
But when trying to insert just Values (...) it works ok.
How do I get it right?
It appears that the data "ApplicationSelection" is already in the database. YOu need to write the select to exclude records that are already in the database. YOu can do that with a where not exists clause or a left join. LOok up teh index to see what field is unique besides the identity. That will tell you what feild you need to check to see if teh record currently exists.

how to insert a column with value row number in an access table

I have a large access table . I need to have a column which will have value as row number
how can I write an sql query fill an empty column inserted by me with row numbers
Write a sequence for your table . Then have default value as sequence .
So, whenever the column does not get users values , it will take the sequence values.
Im sorry, im a little confused at what you're getting at, but could you not just do an alter table?
ALTER TABLE my_table
ADD row_number int;
Make the Field DataType AutoNumber it will do it for you automatically.