Results 1 to 21 of 21

Thread: How to generate a 7 digit autonumbered code?

  1. #1

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Arrow How to generate a 7 digit autonumbered code?

    Hi Guys!

    I need some help here...

    I am still learning vb. I wana generate a 7 digit code (YY-nnnnn) where YY is the current year and nnnnn is an auto-increment number. I am gona use this as a primary key.

    Hope someone can help me with my dilemma.

    Thanks a lot!
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

  2. #2
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: How to generate a 7 digit autonumbered code?

    Use Format((Date),"yy") to get the year part, keep the year and your main number in the registry. Presumably you will be making this into a function so check the registry at the start of the function, if the year changes you know you have a rollover so then reset the number. Failing that use the registry number plus one, then stash the new number in the registry.

    Problem comes if another PC wants to access the same database. In that case you would be better stashing the year and the number somewhere in the database itself. But even that can get messy if you have multiple people accessing the database.

    Best thing to do is let the database do an autonumber for you. Forget the Year thing, add it as a field in the database if you really want to.

    HTH
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  3. #3
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: How to generate a 7 digit autonumbered code?

    Do you mean something like this?:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Dim i As Integer
    5. For i = 100 To 600
    6.     Text1.Text = Text1.Text & Year(Now) & "-" & i & vbCrLf
    7.     DoEvents
    8. Next i
    9. End Sub
    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  4. #4

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Re: How to generate a 7 digit autonumbered code?

    I figured I can use this:
    uniqueworkno.text=FORMAT(curyear, 'YY')&'-'&FORMAT(workno, '#####')

    but i don't know what to do with the increment part....though i also am not sure if the above code will work... =(
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

  5. #5
    Lively Member
    Join Date
    Jul 2004
    Posts
    80

    Re: How to generate a 7 digit autonumbered code?

    do this

    VB Code:
    1. workno = workno + 1

    and then
    VB Code:
    1. uniqueworkno.text=FORMAT(curyear, 'YY')&'-'&FORMAT(workno, '#####')

  6. #6

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Re: How to generate a 7 digit autonumbered code?

    no for loop?
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

  7. #7
    Lively Member
    Join Date
    Jul 2004
    Posts
    80

    Re: How to generate a 7 digit autonumbered code?

    from what i understand you want to make a an id per entery

    so

    when ever u add an entery and need to make an id just obtain the last 5 digits of the last entery and then plus one to it

    the for loop will most likely be used when you want to do something with ALL the id's

  8. #8

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Re: How to generate a 7 digit autonumbered code?

    oh ok...sowi!

    Yes, you're correct! I wana create a unique id everytime I make an entry. =)

    Simple eyh but that works for me!

    Thanks a lot!
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

  9. #9
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: How to generate a 7 digit autonumbered code?

    seraphicmortal,

    You can create a unique id using the API GetTickCount. You can use this as a unique number by appending it to the YY and DD.

  10. #10
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: How to generate a 7 digit autonumbered code?

    Quote Originally Posted by randem
    You can create a unique id using the API GetTickCount. You can use this as a unique number by appending it to the YY and DD.
    Strictly speaking Randem there is a chance that the primary key won't be unique using that.

    GetTickCount retrieves the number of milliseconds that have elapsed since Windows was started. Admitedly the chances of ever hitting a record create at exactly the ame time between sessions is vanishingly small .... but sods law could be a factor here.

    If the application is only going to be used on one machine by one user then saving a number in the registry and incrementing it when you need to will do the trick.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

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

    Re: How to generate a 7 digit autonumbered code?

    Is this for a database, such as ACCESS or MS SQL Server?

    We use MS SQL server and have primary keys of FISCAL YEAR+ENTRY # (2006-000001, 2006-000002, and so on) in our accounting systems.

    *** 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

  12. #12

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Re: How to generate a 7 digit autonumbered code?

    Szlamany,

    Yep, I'm gona use an ACCESS database for this. Can you tell me how you come up with your primary keys pls.....?
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

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

    Re: How to generate a 7 digit autonumbered code?

    We have a control table that we get the "last value" used for a fiscal year and then increment it so that we mark that value used.

    *** 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

  14. #14

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Re: How to generate a 7 digit autonumbered code?

    Huh? Can you show me your SQL code pls? if it's not that much though...
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

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

    Re: How to generate a 7 digit autonumbered code?

    Ok - here's a post with the concept - it's not ACCESS, but MS SQL server - so it's a STORED PROCEDURE that has a bunch of queries that execute as a single transaction. But you should be able to see the concept and translate it to VB and ACCESS...

    http://www.vbforums.com/showpost.php...7&postcount=11

    *** 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

  16. #16

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Re: How to generate a 7 digit autonumbered code?

    hey Szlamany,

    Is it possible for a field (for example workno) that is autonumbered (for auto-increment purposes) to set as a five digit number?? for example.. the 00001 as the first record, 00002 as the second and so on and so forth????

    how do i do this?
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to generate a 7 digit autonumbered code?

    You could probably start with 10000 to get five digits. Otherwise, you would have to format it to get the leading zeros.

  18. #18

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Re: How to generate a 7 digit autonumbered code?

    like what I did in post #4?

    uniqueworkno.text=FORMAT(curyear, 'YY')&'-'&FORMAT(workno, '#####')
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

  19. #19
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to generate a 7 digit autonumbered code?

    Actually, if the field is called workno, then this would work.

    VB Code:
    1. uniqueworkno.text = FORMAT(curyear, "YY") & "-" & _
    2.                              FORMAT(workno, "#####'")

  20. #20

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Re: How to generate a 7 digit autonumbered code?

    ok...i'll give it a go.

    Thanks a lot for the help guys! =)
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

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

    Re: How to generate a 7 digit autonumbered code?

    Quote Originally Posted by seraphicmortal
    like what I did in post #4?
    Doing that means it won't be auto-generated in the database - but processed on the client side.

    *** 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

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