Results 1 to 8 of 8

Thread: [2005] auto-generated account no.. yyyy-9999

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    4

    [2005] auto-generated account no.. yyyy-9999

    i would just like to ask how would be the code for this: yyyy-9999 in vb.net..
    yyyy - for the current year
    9999 - for the record number

    like for instance, for the new record it will be 2008-0001... then for the next record it will be 2008-0002 and so on.. and then if the year ended like for example if the year turns to 2009, the outcome will be 2009-0001... the record will be back to the 0001 and the year will be automatically change to the current year.. that's how it is..

    any suggestion how can i do that? i'm just a beginner in vb.net.. i want your help..thanks in advance.

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

    Re: [2005] auto-generated account no.. yyyy-9999

    vb.net Code:
    1. Dim recNo As String = Date.Now.ToString("yyyy-9999")
    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
    Jul 2008
    Posts
    4

    Re: [2005] auto-generated account no.. yyyy-9999

    Quote Originally Posted by jmcilhinney
    vb.net Code:
    1. Dim recNo As String = Date.Now.ToString("yyyy-9999")

    when i do that.. it will be automatically back to 0001 record for the next year?

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

    Re: [2005] auto-generated account no.. yyyy-9999

    Sorry. I probably should have read your first post a bit more carefully. I really only showed how to format the date into the value. To do what you ask you would need to store the last year used and the last record number used, probably in a database so that they are persisted between sessions. You'd get those values into variables and then when you wanted to generate a new record you'd do something like this:
    vb.net Code:
    1. Dim currentYear as Integer = Date.Now.Year
    2.  
    3. If currentYear = lastYear Then
    4.     'The current year is the same as the last year used so increment the record number.
    5.     recordNumber += 1
    6. Else
    7.     'The current year is later than the last year used so update the last year and reset the record number.
    8.     lastYear = currentYear
    9.     recordNumber = 1
    10. End If
    11.  
    12. Dim recordID As String = String.Format("{0}-{1:0000}", lastYear, recordNumber)
    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    4

    Thumbs up Re: [2005] auto-generated account no.. yyyy-9999

    Quote Originally Posted by jmcilhinney
    Sorry. I probably should have read your first post a bit more carefully. I really only showed how to format the date into the value. To do what you ask you would need to store the last year used and the last record number used, probably in a database so that they are persisted between sessions. You'd get those values into variables and then when you wanted to generate a new record you'd do something like this:
    vb.net Code:
    1. Dim currentYear as Integer = Date.Now.Year
    2.  
    3. If currentYear = lastYear Then
    4.     'The current year is the same as the last year used so increment the record number.
    5.     recordNumber += 1
    6. Else
    7.     'The current year is later than the last year used so update the last year and reset the record number.
    8.     lastYear = currentYear
    9.     recordNumber = 1
    10. End If
    11.  
    12. Dim recordID As String = String.Format("{0}-{1:0000}", lastYear, recordNumber)
    thanks for the reply..it was a great help for me..

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    4

    Re: [2005] auto-generated account no.. yyyy-9999

    can you help me?

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [2005] auto-generated account no.. yyyy-9999

    Where are you storing the "current year" in use?

    Is this all in a database?

    How do you expect to get the "last sequence number" that was used for the year desired?

    Note that we do exactly what you are asking in our financial applications - but without know a bit more about your situation it would be useless for me to show you how we accomplish this.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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

    Re: [2005] auto-generated account no.. yyyy-9999

    Quote Originally Posted by ashley_10
    can you help me?
    With what? I thought I already had.
    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