Solved

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

Posted on 2011-09-15
5
224 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
  • 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

22 Experts available now in Live!

Get 1:1 Help Now