Generating Random Number/ID


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Generating Random Number/ID

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Posts
    4

    Generating Random Number/ID

    I am wondering if someone can lend me a hand. I am trying to create a random Id using letters, dates and random letters/numbers to look like this:

    B = Random Letter
    20051103 = Current Year, Month, Day
    1T4SZ = Random Letters and Numbers

    B200511031T4SZ is how it would look when it is entered into my table.


    I am struggling on how to do this.

    Can someone lend me a hand on how to create this?

    Basically I will update this field up my update to my table from a form.

    I am writing this back to an Access Database. I need help generating the Id and the concanation.

    Thanks for any help that can be provided.

  2. #2
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    to generate a random letter "A" to "Z"

    chr(65+fix(rnd*26))

    a random number "0" to "9"

    chr(48+fix(rnd*10))

    to format the date:

    format(date,"yyyymmdd")

    Marco
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  3. #3
    Join Date
    Nov 2005
    Posts
    4
    While I am looking at this I see you ended this with the word Macro.

    Why Macro? I have not seen that before? I have tried writing this and I seem to be having some issues.

    Basically when I put this in my form load to populate my text box I am not getting any thing to work.

    I thank you for your help.

  4. #4
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    Read it again: it is MARCO, and that is my name. I like to sign my posts

    You are welcome.

    Marco
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  5. #5
    Join Date
    Nov 2005
    Posts
    4
    Thanks and my apologies. I am at work early and guess my reading is not all that great. Here is what I ended up doing to create my number:

    Public Function GenPrimaryValue() as String

    CrntDate = Now
    upperbound = 89
    lowerbound = 65
    intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)

    strStart = Chr(intChar)

    strDate = DatePart("YYYY", CrntDate)
    strDate = strDate & Format(DatePart("m", CrntDate), "00")
    strDate = strDate & Format(DatePart("d", CrntDate), "00")

    strEnd = ""
    For X = 1 To 5
    upperbound = 0
    lowerbound = 2
    intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
    Select Case intChar
    Case 1
    upperbound = 89
    lowerbound = 65
    intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
    Case Else
    upperbound = 56
    lowerbound = 48
    intChar = CInt((upperbound - lowerbound + 1) * Rnd + lowerbound)
    End Select
    strEnd = strEnd & Chr(intChar)
    Next X

    GenPrimaryValue = strStart & strDate & strEnd

Similar Threads

  1. Random Numbers on button
    By daina in forum Java
    Replies: 3
    Last Post: 10-12-2005, 01:35 AM
  2. Database & Random Numbers
    By Suze in forum VB Classic
    Replies: 2
    Last Post: 05-10-2005, 03:41 PM
  3. random() and srandom() unresolved links
    By ozmanbozman in forum C++
    Replies: 25
    Last Post: 02-17-2005, 06:39 AM
  4. generating unique random numbers
    By Dave in forum Java
    Replies: 3
    Last Post: 05-14-2002, 08:04 PM
  5. random numbers
    By max in forum Java
    Replies: 2
    Last Post: 04-30-2001, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center