Results 1 to 9 of 9
  1. #1
    MTSPEER is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    283

    Random Numbers


    Hello,

    I was wondering if you guys can help me write a query to generate random number in a field I have in my database. The field name is HICN, and the table name is Test_Table. I have records already in the table, hundreds of them. Also, the random number has to have 9 numbers and 1 letter at the end of the number.

    Thank you!

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    What have you tried? What result?

    There is a sample here showing the generation of a random number.

  3. #3
    MTSPEER is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    283
    Haven't tried yet, I'm confused on how to get the letter at the end

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Pick a random number between 1 and 26, and select a letter from the alphabet (abc...xyz)

    Here's a function that you could use.

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : randomNumber
    ' Author    : Jack
    ' Created   : 11/18/2010
    ' Purpose   : To Generate Random numbers between and including a range of numbers.
    'Lo and Hi are the lowest and highest random numbers you wish to generate.
    'The Randomize keyword is critical to getting different results for each Access session.
    '---------------------------------------------------------------------------------------
    ' Last Modified:
    '
    ' Inputs: N/A
    ' Dependency: N/A
    '------------------------------------------------------------------------------
    '
    Function randomNumber(Lo As Integer, Hi As Integer) As Integer
    10       On Error GoTo random_Error
    20    Randomize
    30    randomNumber = Int((Hi - Lo + 1) * Rnd - Lo)
    
    40       On Error GoTo 0
    50       Exit Function
    
    random_Error:
    
    60        MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure random of Module AccessMonster"
    End Function
    Three samples
    ?randomnumber(1,26)
    1
    7
    16

  5. #5
    MTSPEER is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    283
    I don't want to have to pick a random number for each row, I just want to update the whole field with ranom numbers from clicking one query.

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Change the Integer assignments to Long in the RandomNumber function;
    then try this
    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : TestAlphaRandom
    ' Author    : mellon
    ' Date      : 15/12/2014
    ' Purpose   :To pick a random number with a single, terminating random alpha
    '---------------------------------------------------------------------------------------
    '
    Sub TestAlphaRandom()
        Dim a(26) As String
        Dim i As Integer
        Dim jNum As Double
        Dim klong As Long
       On Error GoTo TestAlphaRandom_Error
      klong = 999999999
        For i = 1 To 26
            a(i) = Chr(64 + i)
        Next i
        jlong = randomNumber(1, klong)
        Debug.Print jlong & a(randomNumber(1, 26))
    
       On Error GoTo 0
       Exit Sub
    
    TestAlphaRandom_Error:
    
        MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure TestAlphaRandom of Module AccessMonster"
    End Sub
    Here are a few samples


    881858287Q
    609171448T
    381262479H
    814044771M

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Did you try the routine in post#6 ?

  8. #8
    MTSPEER is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    283
    Sorry I am kinda new to this, can I put this into an update query?

  9. #9
    MTSPEER is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    283
    Also, I am having trouble thinking of a way to generate last names. They can all be Sara, but have them with numbers at the end. Like Sara1111, Sara1112 and so on. If you can help me out that would be awesome

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 0
    Last Post: 03-17-2012, 01:06 AM
  2. Access generating random numbers
    By dama in forum Forms
    Replies: 1
    Last Post: 02-19-2012, 06:36 PM
  3. Random Text and Numbers
    By Mclaren in forum Programming
    Replies: 5
    Last Post: 11-05-2010, 04:34 AM
  4. Random numbers
    By Gator777 in forum Access
    Replies: 3
    Last Post: 08-12-2009, 10:04 AM
  5. generating random numbers on form
    By anitra in forum Forms
    Replies: 1
    Last Post: 02-14-2006, 10:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums