Results 1 to 6 of 6

Thread: Auto generating ID

  1. #1

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Auto generating ID

    Hello sir,
    I am using msaccess 2003.
    i am having two fields
    Username
    Key


    When i enter any data in field "UserName" means, the "key" field must generates a key automatically(Generate a 5X5 Random key(number+alphabets))

    How to do this?

    Thankyou

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

    Re: Auto generating ID

    By far the easiest way is to set the data type of the Key field to AutoNumber, however that will be just numeric (but at least it will be unique, unlike what you want).

    To have what you want would require a trigger, which Access doesn't support... but there might be a method that somebody knows of.

  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Auto generating ID

    You may use AutoNumber (Random) for Key (in range -2147483648 to 2147483647) but output it as a unique Hex string of maximum of 8 characters:
    HKey = Right$("00000000" & Hex$(Key), 8)

    When input is needed, convert Hex string back to Long:
    Key = CLng("&H" & HKey)
    Code:
        Key        HKey
    -1332850389  B08E512B
     -115185084  F9226A44
     1377421359  5219C82F
     1171915686  45DA03A6
     1224652377  48FEB659
    -1491361391  A71BA191
     -455462448  E4DA31D0
     -959114901  C6D5116B
    -1725457355  99279C35
     1610662392  6000C1F8
       56385591  035C6037
    Instead of using Hex, you can use a more complicated way to convert back and fort between Long AutoNumber and KeyString.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Auto generating ID

    Quote Originally Posted by anhn
    You may use AutoNumber (Random) for Key (in range -2147483648 to 2147483647) but output it as a unique Hex string of maximum of 8 characters:
    HKey = Right$("00000000" & Hex$(Key), 8)

    When input is needed, convert Hex string back to Long:
    Key = CLng("&H" & HKey)
    Code:
        Key        HKey
    -1332850389  B08E512B
     -115185084  F9226A44
     1377421359  5219C82F
     1171915686  45DA03A6
     1224652377  48FEB659
    -1491361391  A71BA191
     -455462448  E4DA31D0
     -959114901  C6D5116B
    -1725457355  99279C35
     1610662392  6000C1F8
       56385591  035C6037
    Instead of using Hex, you can use a more complicated way to convert back and fort between Long AutoNumber and KeyString.
    Thankyou for your reply. I tried this but i dont know how to set this, while designing the table by design view.
    I made the field type to autonumber, but after that how to do?
    Thankyou

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Auto generating ID

    Data Type: AutoNumber
    Field Size: Long Integer
    New Values: Random
    Indexed: Yes (No Duplicates)
    Attached Images Attached Images  
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  6. #6

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Auto generating ID

    Quote Originally Posted by anhn
    Data Type: AutoNumber
    Field Size: Long Integer
    New Values: Random
    Indexed: Yes (No Duplicates)
    Thankyou for your reply.

    But i set that, but i dont know where to set this value.

    HKey = Right$("00000000" & Hex$(Key), 8)

    Key = CLng("&H" & HKey)

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