Results 1 to 6 of 6
  1. #1
    TToc2u is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2011
    Posts
    9

    Generate random table and display top three results.

    I am failry new to access, but am pretty computer savy.



    I am trying to create a query that pulls three random DISTINCT records from a table. I am able to get the top three random records ok, but I cant figure out how to make sure they are Distinct records.

    I would like them to be disticnt animals from the "animal column"

    This is what I have:

    SELECT TOP 3 Enrichment.Animal, Enrichment.Catagory, Enrichment.Item, Enrichment.Comments
    FROM Enrichment
    GROUP BY Enrichment.Animal, Enrichment.Catagory, Enrichment.Item, Enrichment.Comments, Enrichment.ID, Rnd([ID])
    ORDER BY Rnd([ID]);


    I will then want to creat a form that has three text boxs. One displays informaiton from row 1, the second dissplays information from row 2 , and the third displays information from row 3.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    This seems to do something, requires a unique ID field in source table:
    SELECT TOP 3 Int(Rnd([ID])*6) AS RandomNum, Enrichment.Animal, Enrichment.Catagory, Enrichment.Item, Enrichment.Comments
    FROM (SELECT Max(ID) AS MaxOfID FROM Enrichment GROUP BY Animal) As MaxEnrichmentID
    LEFT JOIN Enrichment ON MaxEnrichmentID.MaxOfID = Enrichment.ID
    ORDER BY Int(Rnd([ID])*6);

    This might be better http://access.mvps.org/access/queries/qry0011.htm

    What do you mean by three textboxes, one for each row? Textboxes display fields. Build form that has textbox for each field, set form to Continuous or Datasheet view. The records will display in tabular arrangement.
    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.

  3. #3
    TToc2u is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2011
    Posts
    9
    Thanks for helping out. That SQL string works great except that it generates the same 3 or 4 results each time the query is first loaded. I tried to modify the string you have me by multiplying it by 100 instead of 6, but it didnt help. Should I do a NEW ID function?

    Code:
    SELECT TOP 3 Enrichment.Animal, Enrichment.Catagory, Enrichment.Item, Enrichment.Comments, Enrichment.[Rating (1-3)]
    FROM (SELECT Max(ID) AS MaxOfID FROM Enrichment GROUP BY Animal) AS MaxEnrichmentID LEFT JOIN Enrichment ON MaxEnrichmentID.MaxOfID = Enrichment.ID
    ORDER BY Int(Rnd([ID])*100);

  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,726
    See this article, especially the RANDOMIZE statement,

    http://www.techrepublic.com/blog/how...oft-access/149

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Interesting, test with my data seemed to work. That is a great article Orange. A complication of OP's requirement is they don't want multiple records with same animal. My suggestion was attempting to accommodate that. I knew that always pulling the Max ID for each animal was not a totally random process but I am at a loss on how to approach that restriction.
    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.

  6. #6
    TToc2u is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2011
    Posts
    9

    Thumbs up

    I was able to generate a random list of records by using the following:

    Code:
     
    Function Randomizer() As Integer
    Static AlreadyDone As Integer
    If AlreadyDone = False Then Randomize: AlreadyDone = True
    Randomizer = 0
    End Function

    Code:
     
    SELECT Top 3 Rnd(IsNull([enrichment].[ID])*0+1) AS Expr1, Enrichment.Animal, Enrichment.Catagory, Enrichment.Item, Enrichment.Comments, Enrichment.[Rating (1-3)]
    FROM Enrichment
    WHERE (((Randomizer())=0))
    GROUP BY Rnd(IsNull([enrichment].[ID])*0+1), Enrichment.Animal, Enrichment.Catagory, Enrichment.Item, Enrichment.Comments, Enrichment.[Rating (1-3)]
    ORDER BY Rnd(IsNull([enrichment].[ID])*0+1);

    This works really well but I am still unable to specify DISTINCT values of "animals" basically I want it to look like this

    From this:
    Code:
    1, a, c,
    1, r, t, 
    2, h, e,
    3, l, d,
    Ect...
    To This:
    Code:
    1, r, t, 
    2, h, e,
    3, l, d,

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

Similar Threads

  1. Replies: 6
    Last Post: 05-14-2012, 07:24 AM
  2. Display Query Results on a Form
    By P5C768 in forum Queries
    Replies: 5
    Last Post: 05-04-2010, 11:04 AM
  3. Replies: 2
    Last Post: 04-02-2010, 07:42 AM
  4. display query results in a form
    By P5C768 in forum Queries
    Replies: 3
    Last Post: 08-14-2009, 03:02 PM
  5. Form to display results
    By Zholt in forum Forms
    Replies: 9
    Last Post: 05-07-2009, 10:09 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