Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Generating Unique Alpha-Numeric ID's?

Generating Unique Alpha-Numeric ID's?

Scheduled Pinned Locked Moved Visual Basic
helptutorialquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DarynRoberts
    wrote on last edited by
    #1

    Hi all...Im hoping someone can help us. For our project, we need to obviously create unique ID's in our tables as one always has to. But we want to include alphabetic characters. For example, in our landlord subsystem, it should generate LL002. Currently we simply just loop through the collection, add one more, and then concatenate it with the "LL". However, this seems to work sometimes and not others...so it doesnt seem very stable. Is there any other way to do it? oh- we using visual basic 2005 thanks

    Dazz

    N G D 3 Replies Last reply
    0
    • D DarynRoberts

      Hi all...Im hoping someone can help us. For our project, we need to obviously create unique ID's in our tables as one always has to. But we want to include alphabetic characters. For example, in our landlord subsystem, it should generate LL002. Currently we simply just loop through the collection, add one more, and then concatenate it with the "LL". However, this seems to work sometimes and not others...so it doesnt seem very stable. Is there any other way to do it? oh- we using visual basic 2005 thanks

      Dazz

      N Offline
      N Offline
      nlarson11
      wrote on last edited by
      #2

      did you try taking the (max) from the table and then add 1 to it?

      D 1 Reply Last reply
      0
      • D DarynRoberts

        Hi all...Im hoping someone can help us. For our project, we need to obviously create unique ID's in our tables as one always has to. But we want to include alphabetic characters. For example, in our landlord subsystem, it should generate LL002. Currently we simply just loop through the collection, add one more, and then concatenate it with the "LL". However, this seems to work sometimes and not others...so it doesnt seem very stable. Is there any other way to do it? oh- we using visual basic 2005 thanks

        Dazz

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        DarynRoberts wrote:

        However, this seems to work sometimes and not others...

        Standard question #1: What do you mean by "not working"?

        --- single minded; short sighted; long gone;

        M 1 Reply Last reply
        0
        • N nlarson11

          did you try taking the (max) from the table and then add 1 to it?

          D Offline
          D Offline
          DarynRoberts
          wrote on last edited by
          #4

          Yes we did try take the max out, but then the problem comes in when a record is deleted. For example, if we have ten records but number five is deleted, then since there is now nine records, the next ID will be number 10. But number 10 still exists. We then tried to simply generate a random number but there is something wrong with our block of code as only the 3 same numbers keeps getting generated.

          Dazz

          N 1 Reply Last reply
          0
          • G Guffa

            DarynRoberts wrote:

            However, this seems to work sometimes and not others...

            Standard question #1: What do you mean by "not working"?

            --- single minded; short sighted; long gone;

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            Heres an idea, assuming SQL Server Add 3 fields to the table IDNo int Identity(1,1) Prefix char(2) default 'LL' TableID varchar(20) Add a calc in your insert proc to add prefix & IDNo together and stick into TableID. I think the IDNo may only be generated by the commit of the insert transaction. You can probably do the last step with a trigger but I loathe triggers so would not recommend it. OR do a select top 1 on the id, increment and save with the prefix, caveat this will be nasty on a high use system.

            G 1 Reply Last reply
            0
            • M Mycroft Holmes

              Heres an idea, assuming SQL Server Add 3 fields to the table IDNo int Identity(1,1) Prefix char(2) default 'LL' TableID varchar(20) Add a calc in your insert proc to add prefix & IDNo together and stick into TableID. I think the IDNo may only be generated by the commit of the insert transaction. You can probably do the last step with a trigger but I loathe triggers so would not recommend it. OR do a select top 1 on the id, increment and save with the prefix, caveat this will be nasty on a high use system.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Perhaps you should reply the original poster instead of me...

              --- single minded; short sighted; long gone;

              1 Reply Last reply
              0
              • D DarynRoberts

                Yes we did try take the max out, but then the problem comes in when a record is deleted. For example, if we have ten records but number five is deleted, then since there is now nine records, the next ID will be number 10. But number 10 still exists. We then tried to simply generate a random number but there is something wrong with our block of code as only the 3 same numbers keeps getting generated.

                Dazz

                N Offline
                N Offline
                nlarson11
                wrote on last edited by
                #7

                If you use the Max(column) to insert all your records then the behavior that you speak of wouldn't happen. Meaning, if you delete a record, the key for each record has not been altered. So if you take the Max(column) for the new record the answer will be LL010 still and you add 1 to it. So you will be inserting the new record with the key of LL011. Select Max(KeyID) From Table 'returns LL010 add 1 and insert new with LL011 You would only be inserting LL010 again if the user deleted LL010 otherwise adding one will always result in a non-duplicate key.

                D 1 Reply Last reply
                0
                • D DarynRoberts

                  Hi all...Im hoping someone can help us. For our project, we need to obviously create unique ID's in our tables as one always has to. But we want to include alphabetic characters. For example, in our landlord subsystem, it should generate LL002. Currently we simply just loop through the collection, add one more, and then concatenate it with the "LL". However, this seems to work sometimes and not others...so it doesnt seem very stable. Is there any other way to do it? oh- we using visual basic 2005 thanks

                  Dazz

                  D Offline
                  D Offline
                  DarynRoberts
                  wrote on last edited by
                  #8

                  Just wanted to say thanks to everybody who gave input. ciao.

                  Dazz

                  1 Reply Last reply
                  0
                  • N nlarson11

                    If you use the Max(column) to insert all your records then the behavior that you speak of wouldn't happen. Meaning, if you delete a record, the key for each record has not been altered. So if you take the Max(column) for the new record the answer will be LL010 still and you add 1 to it. So you will be inserting the new record with the key of LL011. Select Max(KeyID) From Table 'returns LL010 add 1 and insert new with LL011 You would only be inserting LL010 again if the user deleted LL010 otherwise adding one will always result in a non-duplicate key.

                    D Offline
                    D Offline
                    DarynRoberts
                    wrote on last edited by
                    #9

                    thanks dude. that helped

                    Dazz

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups