Solved

How do I create a query in MS Access 2007 to select a random Value?

Posted on 2011-09-15
5
256 Views
Last Modified: 2013-11-05
I have tried the below but am not getting anywhere.  Last week the below formula worked,b ut now it doesn't.  Is there a better way to random a formula for a MS Access Query to pull for me a random value?


Int ((6 - 1 + 1) * Rnd + 1)

would return a random number between 1 and 6



Int ((200 - 150 + 1) * Rnd + 150)

would return a random number between 150 and 200



Int ((999 - 100 + 1) * Rnd + 100)

would return a random number between 100 and 999
0
Comment
Question by:Wraith300
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36543661
Please explain what you mean by "doesn't work".
0
 

Author Comment

by:Wraith300
ID: 36543973
When I attempt to run it, I receive  compile error message:  "Compile error in query expression"  then it shows whatever the criteria in the query was ex.  1: Int((7-2+1)*Rnd()+20)
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36544121
What was the full SQL statement?
0
 
LVL 30

Accepted Solution

by:
hnasr earned 500 total points
ID: 36545670
Try this:

for table a (f1, ...)

SELECT a.f1, rndNo(7,1,[f1]) AS randomNo
FROM a;

f1 is added to force calculation for each record.

Where rndNo is a unction in a module:

 Function rndNo(up As Integer, lo As Integer, f1 As Integer) As Integer
    Dim x As Integer
    rndNo = Int((up - lo + 1) * Rnd + lo)
 End Function
0
 

Author Comment

by:Wraith300
ID: 36545982
After additional testing, it turned out the actual database had an issue that caused formulas to drop the last characters.  I created a new database, used the same formula and everything is working again.  Thanks for the help
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

740 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