Results 1 to 5 of 5

Thread: When opening Form auto generate Next number in field from SQL List

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    14

    When opening Form auto generate Next number in field from SQL List

    Hey I am REALLY new at VB and teaching myself as I go. I have run into an issue and not been able to find a solution so far. What I want to do is when I open my new work order form the form generates the next WO number. I have already setup my SQL column as Identity and Set the increment to 1 and Identity seed to 1000. I linked the text box to the WO column but I don't know what code to write so that when it opens it pulls the next WO number.

    Thanks for your help
    Ray

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: When opening Form auto generate Next number in field from SQL List

    The reason that you don't know what code to write is because there is no code to write. The ID is not generated until the record is inserted into the database. If you're using a DataTable then it will generate a temporary ID that may well be the same as the final ID but you cannot rely on it being so and should therefore never use it as anything other than a temporary value. It's there more to support foreign keys than anything else.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    14

    Re: When opening Form auto generate Next number in field from SQL List

    Dang I was hoping there was a way to get it to do this so that the person making the new entry could see the WO number before they saved and closed the form.

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

    Re: When opening Form auto generate Next number in field from SQL List

    I understand why that seems like a nice idea, but it has a bad side that means it isn't really worth it.

    What happens if they write the number down and use it, without actually saving the record?

    The number will be wrong (refer to a different record), so there is a very good chance they will accidentally cause the other record to be edited when it shouldn't be - thus causing data errors (for both 'copies' of the record).


    There is also the issue that if two people are in the process of adding a record at the same time, do they get the same number? (even if they don't, what happens if the record with the lower number is not saved?)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: When opening Form auto generate Next number in field from SQL List

    Quote Originally Posted by RWJ_2006 View Post
    Dang I was hoping there was a way to get it to do this so that the person making the new entry could see the WO number before they saved and closed the form.
    There is an option that makes it appear to work the way you want from the user's perspective but requires a bit of a hack under the hood. You could save the record as soon as the user indicates that they want to create one, in which case the ID will be generated straight away. The user can then enter their data and when they save the record in the UI, your app will actually update the existing record rather than insert a new one. In that case though, you will have to allow nulls in all your columns in the database because the record will be completely empty when you insert it. You will also have to ensure that the record gets deleted if the user closes the Add dialogue without saving the record. I wouldn't recommend that course of action unless you absolutely MUST have the ID before saving the record.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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