Re: Autonumber in Database?
This would work in MS SQL. Maybe you can convert it to Access.
Code:
declare @leadid int
set @leadid = isnull((select max(leadid) from YourTable with (updlock)),0) + 1
insert into Yourtable(leadid, other stuff)
values (@leadid, other stuff)
It doesn't address "and without using an existing number being used". But I don't now exactly what you mean by that.
Re: Autonumber in Database?
Thread moved to the 'Database Development' forum (the 'VB6' forum is only meant for questions which don't fit in more specific forums)
Re: Autonumber in Database?
go into the table design, add a new field called LeadID2 and make it an autonumber... save changes, close the database... backup (copy) the database ... then go back into the table design and delete the leadid field, rename LeadID2 to LeadID ... and then save changes again...
That should do it. I know that when I make similar kinds of changes, behind the scenes that's what SQL Server does... so I can only imagine that Access would work in a similar fashion.
It certainly can't hurt to try... just make sure you have a backup/copy in case something does go south.
-tg
Re: Autonumber in Database?
Make sure the new LeadID has the same values as the old to start since it is a foreign key. You don't want to orphan the children :blush:
Re: Autonumber in Database?
ooooooh.... snap... I didn't think about that... good catch... Now I'm not sure how to handle that properly.
-tg
Re: Autonumber in Database?
Quote:
Originally Posted by
techgnome
ooooooh.... snap... I didn't think about that... good catch... Now I'm not sure how to handle that properly.
-tg
Off the top of my head ...speed thinking, which can kill you :D
Copy off the table, add the new column (not identity), update the new column to the old leadid, delete the contents of the current table, add the new LeadID as identity , set identity insert on, load the current table using the new LeadID in the new identity column from the copied table, set identity insert back off.
That is MS SQL and I would definately test it first.
Re: Autonumber in Database?
I'm thinking in MSSQL but can't you turn on the identity and manually next the next seed?
Re: Autonumber in Database?
You have to implement a 'custom counter'.
HOW TO Implement Multiuser Custom Counters in Jet 4.0 and ADO 2.1
http://support.microsoft.com/default...b;EN-US;240317
:wave:
Re: Autonumber in Database?
Quote:
Originally Posted by
gibra
That's pretty much doing what I posted but locking the table for the time it takes to generate the key and then come back and update the record. I think putting the code I posted in a stored procedure would do the same thing. Or using a start and end trans in sequence.
None of that is as clean as taking the time to make the column auto number.