MS Access Forum / Queries / November 2007
Randomize records then select TOP (x)
|
|
Thread rating:  |
banem2@gmail.com - 11 Nov 2007 13:28 GMT Task is simple, but it appear as very though to complete.
Table:
Table1, IDRecord (may be AutoNumber)
record values: 1, 2, 3, 4, 5
This numbers needs to be randomized Rnd(), then extract TOP(n) where "n" is number on form (Form1!Field1).
To select TOP(n) I am using workaround like this:
SELECT Table1.IDRecord, DCount("IDRecord","Table1","IDRecord <= " & [IDRecord]) AS TopNum, Table1.* FROM Table1 WHERE (DCount("IDRecord","Table1","IDRecord <= " & [IDRecord])<=Val(Forms!Form1!Field1[]));
All works fine until I put RND(RecordID) command and sort by this number.
If I first randomize records and use second query, it won't create record number in query in order they appear on screen.
Any solution to randomize records, to create record numbers in query and filter by TOP(n)?
Goal:
IDRecord, RecNo 1, 1 5, 2 3, 3 4, 4 2, 5
Filter on RecNo by form to have only first "n" records.
Thanks!
Douglas J. Steele - 11 Nov 2007 13:42 GMT See http://www.mvps.org/access/queries/qry0011.htm at "The Access Web"
 Signature Doug Steele, Microsoft Access MVP http://I.Am/DougSteele (no private e-mails, please)
> Task is simple, but it appear as very though to complete. > [quoted text clipped - 37 lines] > > Thanks! banem2@gmail.com - 11 Nov 2007 14:06 GMT On Nov 11, 2:42 pm, "Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote:
> Seehttp://www.mvps.org/access/queries/qry0011.htmat "The Access Web" > > -- > Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele > (no private e-mails, please) Sorry, but this is not what I am looking for. I know to randomize records, but I need to return TOP(n). "n" is variable value on form as noted.
banem2@gmail.com - 11 Nov 2007 15:25 GMT On Nov 11, 3:06 pm, ban...@gmail.com wrote:
> On Nov 11, 2:42 pm, "Douglas J. Steele" > [quoted text clipped - 8 lines] > records, but I need to return TOP(n). "n" is variable value on form as > noted. To be more precise: these 2 actions cannot work together.
- If I make a query to return TOP(n) records, then they are not randomized - If I randomize records, then I cannot return TOP(n) records
Dirty workaround is to create temporary table and then use solution with TOP(n), but I don't like this solution.
Douglas J. Steele - 11 Nov 2007 17:29 GMT Sorry, I missed the fact that you wanted Top n to be parameterized.
As far as I know, it cannot be done in Access. You'd have to rewrite the SQL for a stored query, and then run the stored query.
 Signature Doug Steele, Microsoft Access MVP http://I.Am/DougSteele (no private e-mails, please)
> Sorry, but this is not what I am looking for. I know to randomize > records, but I need to return TOP(n). "n" is variable value on form as > noted. banem2@gmail.com - 11 Nov 2007 21:05 GMT On Nov 11, 6:29 pm, "Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote:
> Sorry, I missed the fact that you wanted Top n to be parameterized. > [quoted text clipped - 12 lines] > > records, but I need to return TOP(n). "n" is variable value on form as > > noted. Yes, you are right... but how to do that? :)
Douglas J. Steele - 11 Nov 2007 21:34 GMT Dim qdfCurr As DAO.QueryDef Dim strSQL As String
If Len(Forms!Form1!Field1 & vbNullString) > 0 Then strSQL = "Select Top " & Forms!Form1!Field1 & _ " MyTable.* From MyTable Where Randomizer() = 0 " & _ "Order By Rnd(IsNull(MyTable.Question) * 0 + 1)" Set qdfCurr = CurrentDb.QueryDefs("MyRandomQuery") qdfCurr.SQL = strSQL End If
 Signature Doug Steele, Microsoft Access MVP http://I.Am/DougSteele (no private e-mails, please)
> On Nov 11, 6:29 pm, "Douglas J. Steele" > <NOSPAM_djsteele@NOSPAM_canada.com> wrote: [quoted text clipped - 17 lines] > > Yes, you are right... but how to do that? :) banem2@gmail.com - 11 Nov 2007 22:25 GMT On Nov 11, 10:34 pm, "Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote:
> Dim qdfCurr As DAO.QueryDef > Dim strSQL As String [quoted text clipped - 36 lines] > > > Yes, you are right... but how to do that? :) Thanks, this will help!
|
|
|