Solved

access 2007 randomize

Posted on 2014-07-30
8
245 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 45

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 - Access MVP) earned 500 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 45

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 49

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 49

Expert Comment

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

/gustav
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now