Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

access 2007 randomize

Posted on 2014-07-30
8
Medium Priority
?
258 Views
Last Modified: 2014-08-07
I am trying to create a randomize function in access2007.  I would like to run a query to return 100 rows from my table. I would like query to the table an get a different result set  everytime i run the query.  Help is needed.  I am including the database i have started.
0
Comment
Question by:centralmike
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 40230255
If you add a column to your query, you can simulate randomness via the VB pseudo-random number function, Rnd().  Pass it the row's key value.
Example:
SELECT ID, Rnd([ID]) AS RndSortValue
FROM MyTable
ORDER BY Rnd([ID]);

Open in new window

Be aware that the VB PRNG functions are flawed.  I wrote an analysis in this article:
http:A_11114.html
0
 

Author Comment

by:centralmike
ID: 40230520
The random number columns changes.  But it returns the same 100 rows each time you run the query.
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 2000 total points
ID: 40230645
Here is an example that avoids the issue above - which I've used many times:

SELECT TOP 100 tblEmp.EmpID, tblEmp.EmpName
FROM tblEmp
ORDER BY Rnd(Replace(TimeValue(Now()),":","")*[tblEmp].[EmpID]-Now()*[tblEmp].[EmpID]);
mx
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 75
ID: 40230651
Another similar example:

SELECT TOP 10 tblEmp.EmpID, tblEmp.EmpName
FROM tblEmp
ORDER BY Rnd(Int(Now()*[tblEmp].[EmpID])-Now()*[tblEmp].[EmpID]);

mx
0
 
LVL 46

Expert Comment

by:aikimark
ID: 40230728
@mike

Please post 20 of your ID values.  I'm not seeing that behavior with my table/query.
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40231179
You will if you close Access and open it and run the query again.

/gustav
0
 
LVL 75
ID: 40232252
Guaranteed to not be an issue if you include Now() as shown in my examples.
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40232926
Yes, this is a great tip.

/gustav
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question