Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating With Calculation

Status
Not open for further replies.

imsco

Technical User
Aug 31, 2005
4
US
I need to update a field with a range of random numbers, say between 50,000 and 500,000. How would I do this?

 
hmmm....so it would look like how with the range?

Rnd(50000-500000)????
 
something like this:

expr1: Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)

replace your lower and upperbounds

-DNG
 
this replaced all the records with the same value...need to be random numbers for each record.
 
SQL is optimizing your query and is calling Rnd only once. You need to trick it into making the call for each record retrieved. First write a function
Code:
Public Function MyRnd(x As Variant) As Long
    MyRnd = (500000 - 50000) * Rnd + 50000
End Function
Then write your SQL as
Code:
SELECT myTable.myField, MyRnd(myTable.myField) AS RandomNumber
FROM myTable;
Where you pass any field from your record into the function. It doesn't really matter which one because (as you can see) the value isn't used in the function. It's purpose is to force SQL to evaluate the function for every record.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top