View Single Post
  #2  
Old December 11th, 2009, 02:03 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default Real 10 random records

See http://support.microsoft.com/default.aspx?id=208855

Possible, not quite as easy as it ought to be.

Copy and paste this little function into a module; save the module as
basRandom (anything except RndNum, you can't use the same name twice);


Public Function RndNum(vIgnore as Variant) As Double
Static bRnd As Boolean
If Not bRnd Then
Randomize
bRnd = False
End If
RndNum = Rnd()
End Function

Then include a calculated field in the Query by typing:

Shuffle: RndNum([somefield])

where "somefield" is any numeric field in your table - this just forces Access
to give you a new random number for every row. If you don't have a numeric
field available then you can use RndNum(Len([SomeField])) to force a number to
be generated.

Sort by this field and it will shuffle your data into random order.

Source: John Vinson

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

Ljudmil wrote:
Hi,
I have a query which gives me 10 random records.

SELECT TOP 10 Basis_.t, Basis_.r, Basis_.Basis_code
FROM Basis_
ORDER BY Rnd(Basis_code);

Basis_code is an Auto Number field.
It works fine but when I open the database and run the query I always get
the same “10 random records”. What should I do to get different 10 random
records when I open the database?
Thanks in advance!
Ljudmil