Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.​
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

861 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

26 Experts available now in Live!

Get 1:1 Help Now