Results 1 to 10 of 10

Thread: Autonumber in Database?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Autonumber in Database?

    Hi guys,

    So basically I have a form on VB that adds data to a database... one of the fields in the database is called 'leadid' in the database structure it is not an autonumber.

    throughout my project it makes reference to the field 'leadid' and other tables have fields that are identified by leadid.

    Unfortunately when building the database i forgot to set leadid as autonumber... and then imported my sql database into microsoft access (this was to have all the existing data on the new database)

    When using my form to add a new lead to the database, it needs to add a number to leadid. Is there anyway of doing it without having autonumber and without using an existing number being used?

    As microsoft access wont let me change it to autonumber...

    Thanks
    Jamie

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    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.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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)

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    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

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Autonumber in Database?

    Quote Originally Posted by techgnome View Post
    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

    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.

  8. #8
    Addicted Member
    Join Date
    Oct 2008
    Location
    Califorina
    Posts
    235

    Re: Autonumber in Database?

    I'm thinking in MSSQL but can't you turn on the identity and manually next the next seed?

  9. #9
    gibra
    Guest

    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


  10. #10
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Autonumber in Database?

    Quote Originally Posted by gibra View Post
    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

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width