Results 1 to 8 of 8
  1. #1
    cbrxxrider is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2013
    Location
    Central NY
    Posts
    16

    Question random value generator between 2 dates

    SELECT TOP 1 *
    FROM (SELECT *,
    RND(ID) AS RandomValue
    FROM tblCompletions)
    ORDER BY RandomValue

    I'm trying to add specific dates to a functioning random value generator and it does not like how I've created it. I'm using short dates.

    I've tried this the following and variations of it which gives me a syntax error (missing operator).

    SELECT TOP 1 *
    FROM (SELECT *,
    RND(ID) AS RandomValue
    FROM tblCompletions)
    WHERE completiondate '02/13/2014' and '02/15/2014'
    ORDER BY RandomValue

  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,847
    Can you give a few more details about what you have and what you want?
    What is your table designs for tblCompletions?

  3. #3
    cbrxxrider is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2013
    Location
    Central NY
    Posts
    16
    An example of the table is shown below. I'm trying to add a button, when pushed would give a randomly generated record between the dates 2/13/14 and 2/15/14 or whatever I decide to pull between.
    ID Department EmployeeID Goal CompletionDate Entered By Verified By Comments Points Earned Denied By Denied Reason Denied Date Verified Date
    265 Revenue Audit 213 Accident free for one month (Department) 3/5/2014 455 455 see above 1


    3/5/2014

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,600
    Bing: Access random record select

    Review http://www.fontstuff.com/vba/vbatut02.htm
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    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,847
    How random can it be given your example? 2/13/2014 and 2/15/2014 (You have 2/14/2014 and what else??)

    Here's a randomNumber generator I used on a previous project. It should give you soome ideas.

    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
       On Error GoTo random_Error
    Randomize
    randomNumber = Int((Hi - Lo + 1) * Rnd - Lo)
    
       On Error GoTo 0
       Exit Function
    
    random_Error:
    
        MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure randomNumber of Module "
    End Function

    Here is a test routine to show how to call the randomNumber
    Code:
    Sub mytestOfRandomNumber()
        Dim a As Integer
        Dim z As Integer
        Dim i As Integer
        a = 3
        z = 300
        For i = 1 To 10
            Debug.Print randomNumber(a, z)
        Next i
    End Sub
    Here is the result showing 10 random numbers between 3 and 300
    Code:
    273 
     101 
     23 
     57 
     113 
     180 
     199 
     64 
     187 
     275
    You could call this routine passing integer values based on your form.
    eg
    add a randomNumber of days to your lowest date value using a DateAdd function.

  6. #6
    cbrxxrider is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2013
    Location
    Central NY
    Posts
    16
    I could paste all the data but I only gave one example to save space (wasn't a great example of the date of course because this one falls on 3/5/14). I have records with dates ranging from 2/3/14 to present.

    I'm just trying to learn how to randomly pull 1 record out of say 10 that fall between 2/13/2014 and 2/15/2014. The expression I currently have will correctly pull a random record but I'm continually incorrectly adding the where statement for the dates I'd like this random pull to fall between.

  7. #7
    cbrxxrider is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2013
    Location
    Central NY
    Posts
    16
    Thanks again June7. I've been looking over different articles about random generators and how to pull data between two dates and then trying to combine them. This one might get me there.

  8. #8
    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,847
    Did you look at the sample code?
    Here's an example using 2 Dates.

    Code:
    Sub ARandomNumberTestWithDates()
    Dim LoDate As Date
    Dim HiDate As Date
    Dim I As Integer
    
    LoDate = #2/10/2014# '<------------Lowest Date
    HiDate = #3/25/2014# '<------------Highest Date
    I = DateDiff("d", LoDate, HiDate)
    Debug.Print "Random date between " & LoDate & "  and  " & HiDate; "  is " & DateAdd("d", LoDate, randomNumber(0, I))
    
    End Sub
    I ran the test 4 times with these results

    Random date between 10/02/2014 and 25/03/2014 is 12/03/2014
    Random date between 10/02/2014 and 25/03/2014 is 15/02/2014
    Random date between 10/02/2014 and 25/03/2014 is 22/03/2014
    Random date between 10/02/2014 and 25/03/2014 is 21/02/2014

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

Similar Threads

  1. Appointment Generator
    By cheechootrain in forum Access
    Replies: 15
    Last Post: 04-23-2013, 12:37 PM
  2. Number generator without Autonumber?
    By Megood in forum Programming
    Replies: 7
    Last Post: 07-19-2012, 08:02 PM
  3. Account ID - Generator
    By sschrader1 in forum Queries
    Replies: 4
    Last Post: 04-09-2012, 08:09 AM
  4. Code Generator
    By fpmsi in forum Programming
    Replies: 1
    Last Post: 09-20-2011, 08:46 AM
  5. Sorta Random Serial Number Generator
    By Cuselco in forum Programming
    Replies: 3
    Last Post: 08-27-2010, 12:05 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