Update query: add random dates (1 Viewer)

phinix

Registered User.
Local time
Today, 02:29
Joined
Jun 17, 2010
Messages
130
Guys I need help please!
I have this function I added to modules, that adds random date in given range.. right? Pretty simple:
Code:
Function RandomDateInRange(LowerDate As Date, UpperDate As Date) As Date
    RandomDateInRange = Int((UpperDate - LowerDate + 1) * Rnd + LowerDate)
End Function

Now, I create update query for a date field to be updated to this, I added ID field to give range, so basically I want to update date for ID between 1 and 3000 to a date between 01/08/2010 and 08/09/2010.

Thing is when I add my function to date field "update to" , it runs this random function once and adds same date in all fields.

How can I make it run in loop, so each field will get new random date?

PLEASE HELP!
 

JANR

Registered User.
Local time
Today, 03:29
Joined
Jan 21, 2009
Messages
1,623
If you pass along the recordID to your function then it will return a more random date.

Code:
Function RandomDateInRange(LowerDate As Date, UpperDate As Date, RecID As Long) As Date
    RandomDateInRange = Int((UpperDate - LowerDate + 1) * Rnd + LowerDate)
End Function

RandomDate: RandomDateInRange(#1/1/10#, #12/31/10#, [ID])

JR
 

namliam

The Mailman - AWF VIP
Local time
Today, 03:29
Joined
Aug 11, 2003
Messages
11,695
You would also want to "re-randomize" the randomizer

Code:
Function RandomDateInRange(LowerDate As Date, UpperDate As Date, RecID As Long) As Date
    RandomDateInRange = Int((UpperDate - LowerDate + 1) * Rnd(RecID) + LowerDate)
End Function

And you may want to change the INT part to include the Rnd() because otherwize you end up with a time component that you may not want.
 

Users who are viewing this thread

Top Bottom