Random Records

E

EeOr

Hi I am using this code I found on a previous post, however I dont seem to
be getting a totally random selection of records as I have ran a query
selecting the top 5 10 times and twice the results were identical. Does
anyone know why this is happening or if there is another random function I
could use?

Thanks

Jon
 
J

John W. Vinson

Hi I am using this code I found on a previous post, however I dont seem to
be getting a totally random selection of records as I have ran a query
selecting the top 5 10 times and twice the results were identical. Does
anyone know why this is happening or if there is another random function I
could use?

Thanks

Jon

You didn't post "this code" so I have no idea why you are getting
these results.

Here's some code which randomizes the random number generator prior to
its first use, and it should give you a different set each time. Since
it's a random selection, though, you may get the same record being
retrieved more than once - it's "random selection with replacement" in
old statistics terms.

Put this little function into a Module:

Public Function RndNum(vIgnore As Variant) As Double
Static bRnd As Boolean
If Not bRnd Then
'Initialize the random number generator once only
bRnd = True
Randomize
End If
RndNum = Rnd()
End Function

Then add a calculated field to your Query by typing

Shuffle: RndNum([fieldname])

in a vacant Field cell, where [fieldname] is any field in
your table - this forces Access to give a different random
number for each record.

Sort the query by Shuffle, and set its Top Values property
to the number of records you want to see.

John W. Vinson [MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top