Results 1 to 6 of 6
  1. #1
    rwahdan1978 is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Jun 2024
    Posts
    57

    select random record

    Hi

    I have a select statement to get a random record it is working fine the second time but the first time it runs it is always displaying the first record (not random) why is that?



    Code:
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM starter where asked <> True ORDER BY rnd(StarterID)")

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870

  3. #3
    rwahdan1978 is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Jun 2024
    Posts
    57
    my code is ok and working the second time but when starting the app i am always getting the same record not a different one. It is choosing the 7th record always not sure why and how?

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    As per the link I provided

    Warning: If you don't call the Randomize function before calling the Rnd function, the Rnd function may return the same random number value each time. And therefore, you may not get a truly random number.

    Try adjusting your code to

    Code:
    ....
    Randomize
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM starter where asked <> True ORDER BY rnd(StarterID)")
    ...

  5. #5
    rwahdan1978 is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Jun 2024
    Posts
    57
    Still same issue

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Here's some code to review.

    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 Long, hi As Long) As Long
    10       On Error GoTo randomNumber_Error
    
    20    Randomize
    30    randomNumber = Int((hi - lo + 1) * Rnd + lo)
    
    40       On Error GoTo 0
    randomNumber_Exit:
    50       Exit Function
    
    randomNumber_Error:
    
    60        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure randomNumber of Module AccessMonster"
    70        GoTo randomNumber_Exit
               
    End Function
    Here s a test of the function

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : mytestOfRandomNumber
    ' Author    : Jack
    ' Date      : 11/18/2010
    ' Purpose   : Create 10 random numbers between a and z
    '---------------------------------------------------------------------------------------
    '
    Sub mytestOfRandomNumber()
              Dim a As Long
              Dim z As Long
              Dim i As Integer
    10        a = 1
    20        z = 26
    30        For i = 1 To 15
    40            Debug.Print i & "   " & randomNumber(a, z)
    50        Next i
    
    60       On Error GoTo 0
    70       Exit Sub
    
    mytestOfRandomNumber_Error:
    
    80        MsgBox "Error " & Err.Number & " in line " & Erl & " (" & Err.Description & ") in procedure mytestOfRandomNumber of Module AccessMonster"
    End Sub
    Sample results based on 3 executions of the function:

    1 6
    2 16
    3 26
    4 10
    5 7
    6 1
    7 17
    8 22
    9 22
    10 21
    11 24
    12 16
    13 13
    14 16
    15 5

    1 2
    2 7
    3 4
    4 13
    5 12
    6 23
    7 8
    8 20
    9 16
    10 15
    11 1
    12 10
    13 17
    14 2
    15 22

    1 2
    2 12
    3 1
    4 3
    5 10
    6 8
    7 26
    8 6
    9 19
    10 19
    11 7
    12 11
    13 17
    14 5
    15 15

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

Similar Threads

  1. Select Random Record
    By cybermonkey in forum Access
    Replies: 4
    Last Post: 10-27-2021, 02:42 PM
  2. Replies: 7
    Last Post: 02-20-2014, 02:34 PM
  3. Replies: 0
    Last Post: 11-30-2010, 12:51 PM
  4. Select Random Records Based on Conditions
    By access123 in forum Queries
    Replies: 1
    Last Post: 10-27-2010, 10:25 AM
  5. random record
    By wasim_sono in forum Programming
    Replies: 1
    Last Post: 02-06-2008, 02:42 PM

Tags for this Thread

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