Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
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 …

772 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