Results 1 to 5 of 5
  1. #1
    ortizimo is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jun 2017
    Location
    FL
    Posts
    88

    Query for specific amount of records

    I made a program for studying for certs. It uses flashcards answers only and also multiple choice and can randomize all 600 questions in it, but I would like to create a mini test of random questions and only select 75 questions out of the total of 600 available. I don't think I'm asking the interwebs the right verbiage since I cant seem to find something similar to what I need. This is what I use to randomize my questions in my query table:

    Expr1: Rnd([tblQuestion]![id])

    Any help is appreciated.

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,425
    Try using the TOP predicate. No idea what your query or data looks like so can't say if that will work (as in not return the same 75 records each time).
    e.g. SELECT TOP 75

    https://support.microsoft.com/en-us/...2-3d0bd1a95b07
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    I use a collection object, this way you wont get repeat randoms.
    It takes the table, reads all the INDEX into the collection
    randomly picks 1 record, then removes it from list so it cant be picked again
    sets the Order# on the table
    repeats until the list is complete.




    Code:
       'this code marks  records then flags that many records to be used.
    Public Sub RandomOrder()
    Dim iTot As Long, iRec As Long
    Dim iSample As Integer
    Dim sTbl As String, sSql As String
    Dim rst  'As Recordset
    Dim i As Long
    Dim iRnd As Long, lNdx As Long
    Dim coll As New Collection
    Dim sKey As String
    
    
    DoCmd.SetWarnings False
    sTbl = "[tNAMES]"
    
    
       'clear the order#
    sSql = "update " & sTbl & " set [OrderNum] = null"
    DoCmd.RunSQL sSql
    
    
      '===================================
      'collect all keys to alter their order
      '===================================
    Set rst = CurrentDb.OpenRecordset("select [ClientID] from " & sTbl)
    With rst
       While Not .EOF
           sKey = rst.Fields(0).value & ""
           coll.Add sKey, sKey
           
           .MoveNext
       Wend
    End With
    Set rst = Nothing
    
    
      '===================================
       'pick key at random then set new order for it
      '===================================
    i = 0
    While coll.Count > 0
        i = i + 1
        iRnd = Int(coll.Count * Rnd + 1)
        'Debug.Print iRnd
        
        sKey = coll(iRnd)
        lNdx = CLng(sKey)
        sSql = "update " & sTbl & " set [OrderNum] = " & i & " where [ClientID]= " & lNdx
        DoCmd.RunSQL sSql
    
    
        coll.Remove sKey
    nextNum:
    Wend
    
    
    DoCmd.SetWarnings True
    DoCmd.OpenTable sTbl
    MsgBox "done"
    Set coll = Nothing
    Exit Sub
    Resume nextNum
    End Sub

  4. #4
    ortizimo is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jun 2017
    Location
    FL
    Posts
    88
    thnx...ill try both!

  5. #5
    ortizimo is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jun 2017
    Location
    FL
    Posts
    88
    Before closing this thread...FYI, in your query you can add the amount of items to be randomly picked from a total pool. Under Query Design > Return: XXX and just enter the number you want returned or a %. For random picks enter a blank field in the query and in it enter: Rnd([tblQuestion]![id])

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

Similar Threads

  1. Ho can I limit the amount of records in a table to one
    By Freddie81 in forum Programming
    Replies: 9
    Last Post: 05-13-2018, 08:02 AM
  2. Replies: 5
    Last Post: 09-21-2015, 10:52 AM
  3. Replies: 2
    Last Post: 05-25-2015, 05:02 AM
  4. Replies: 1
    Last Post: 01-24-2013, 05:50 PM
  5. limiting amount of records
    By yaro.kobzar in forum Programming
    Replies: 1
    Last Post: 05-17-2011, 07:01 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
  •  
Other Forums: Microsoft Office Forums