Connecting Tech Pros Worldwide Forums | Help | Site Map

How does Rnd() work? How to store a value.

Newbie
 
Join Date: Feb 2008
Posts: 30
#1: Mar 10 '08
Hi, This is my problem.
A form (startform) is opened, wich is based on a table, called programvariables. This is supposed to be a one-record table, wich will hold some values needed to store user-preferences. The form shows some of these preferences.
Now I want on the event of opening this form, to make a random number (integer), and then to store this number in a field in de table(programvariables), wich field I call res3.
Then the content of the field can/should be shown in the form I just opened.

Problem1: trying Rnd() just gets me the same nummer over and over again. This is of course not wat random is supposed to be. Just wat need I to do to make Acces give me a realy random number?
Problem2: how do I store (without intervention of the user) a number in a table (that is: in the one pre-existing record, not appending a record to the table).

Have patience with me I realy know very little of VB

Already many thanks
Chris

Zwoker's Avatar
Member
 
Join Date: Jul 2007
Location: Melbourne, Australia
Posts: 66
#2: Mar 10 '08

re: How does Rnd() work? How to store a value.


Quote:

Originally Posted by zaankanter

Problem1: trying Rnd() just gets me the same nummer over and over again. This is of course not wat random is supposed to be. Just wat need I to do to make Acces give me a realy random number?

Before calling Rnd for the first time add a Randomize statement to your code. This uses the system time to create a semi-random starting point for your RND function.

Quote:

Originally Posted by zaankanter

Problem2: how do I store (without intervention of the user) a number in a table (that is: in the one pre-existing record, not appending a record to the table).

You can update the existing record using VBA code - I am assuming you have coded this and are not using MS Access to access your data (If you are you probably need a different solution). An simple example of opening, reading and updating a record in a table is shown below. This is using MS Access 2003. I haven't bother showing any of the DIMs etc...

Expand|Select|Wrap|Line Numbers
  1. Randomize
  2. MyTablet.Open "programvariables", CurrentProject.Connection, adOpenStatic, adLockOptimistic
  3. MyTable.MoveFirst
  4. MyTable![res3]=Rnd()
  5. MyTable.Update
  6. MyTable.Close
Does this help?

By the way, in case you don't realise, the Rnd function returns a value between 0 and 1. If you want an interger value, say a randon number between 1 and 10, then you would need to do something like :
Expand|Select|Wrap|Line Numbers
  1. MyTable![res3 ]= Int((Rnd() * 10) +1)
Newbie
 
Join Date: Feb 2008
Posts: 30
#3: Mar 10 '08

re: How does Rnd() work? How to store a value.


Quote:

Originally Posted by Zwoker

Before calling Rnd for the first time add a Randomize statement to your code. This uses the system time to create a semi-random starting point for your RND function.
<.......>
By the way, in case you don't realise, the Rnd function returns a value between 0 and 1. If you want an interger value, say a randon number between 1 and 10, then you would need to do something like :

Expand|Select|Wrap|Line Numbers
  1. MyTable![res3 ]= Int((Rnd() * 10) +1)

I'll try this tomorrow in my own project. Will let you know if it works.
I'm just wondering, why the @@ are the clear and simple statements quoted above (thanks !) not to be found in any helpfunctions of Acces?

Chris
Newbie
 
Join Date: Feb 2008
Posts: 30
#4: Mar 10 '08

re: How does Rnd() work? How to store a value.


Quote:

Originally Posted by Zwoker

Before calling Rnd for the first time add a Randomize statement to your code. This uses the system time to create a semi-random starting point for your RND function.
<.......>
By the way, in case you don't realise, the Rnd function returns a value between 0 and 1. If you want an interger value, say a randon number between 1 and 10, then you would need to do something like :

Expand|Select|Wrap|Line Numbers
  1. MyTable![res3 ]= Int((Rnd() * 10) +1)

I'll try this tomorrow in my own project. Will let you know if it works.
I'm just wondering, why the @@ are the clear and simple statements quoted above (thanks !) not to be found in any helpfunctions of Acces?

Chris
Zwoker's Avatar
Member
 
Join Date: Jul 2007
Location: Melbourne, Australia
Posts: 66
#5: Mar 11 '08

re: How does Rnd() work? How to store a value.


Quote:

Originally Posted by zaankanter

I'm just wondering, why the @@ are the clear and simple statements quoted above (thanks !) not to be found in any helpfunctions of Acces?

I'm not sure what you mean by the @@ in your question?
Which version of MS Access are you using? Everything I mentioned that is a MS Access function is in my help, and I'm using 2003.

Also, a minor point, but there is an extra "t" in the name I declare in the open statement in my example. If you were to copy and paste it as is, it would fail on the MoveFirst. Just remove the extra letter.

Regards,
Zwoker.
Newbie
 
Join Date: Feb 2008
Posts: 30
#6: Mar 12 '08

re: How does Rnd() work? How to store a value.


You don't want to know what @@ means! And yes, its probably my own fault, that I don't know my way around in the helpfunction of Acces. I use 2003.

Although I think your program lines must be the right answer, it doesn't quite work with me yet. These are the lines I programmed (my table has a slightly different spelling, i.e. dutch)
Private Sub Form_Load()

If IsNull(DLookup("[res3]", "programmavariabelen")) Then
MsgBox "res3 is empty"
Randomize
MyTable.Open "programmavariabelen", CurrentProject.Connection, adOpenStatic, adLockOptimistic
MyTable.MoveFirst
MyTable![res3] = Rnd()
MyTable.Update
MyTable.Close

MsgBox (Me![res3])
End If
End Sub
It stops at My.table.open etc.
There I get "object required", but wich object would that be?
Zwoker's Avatar
Member
 
Join Date: Jul 2007
Location: Melbourne, Australia
Posts: 66
#7: Mar 12 '08

re: How does Rnd() work? How to store a value.


Quote:

Originally Posted by zaankanter

You don't want to know what @@ means! And yes, its probably my own fault, that I don't know my way around in the helpfunction of Acces. I use 2003.

Although I think your program lines must be the right answer, it doesn't quite work with me yet. These are the lines I programmed (my table has a slightly different spelling, i.e. dutch)

Private Sub Form_Load()

If IsNull(DLookup("[res3]", "programmavariabelen")) Then
MsgBox "res3 is empty"
Randomize
MyTable.Open "programmavariabelen", CurrentProject.Connection, adOpenStatic, adLockOptimistic
MyTable.MoveFirst
MyTable![res3] = Rnd()
MyTable.Update
MyTable.Close

MsgBox (Me![res3])
End If
End Sub
It stops at My.table.open etc.
There I get "object required", but wich object would that be?

I can only take a guess that you don't have an object set selected in your project, that you need.
When you are in the code window, click on the Tools menu item and choose References. What items do you have ticked there? It could be something missing there... *shrug*

Hopefully someone else reading this can point you (and me) in the right direction.
Newbie
 
Join Date: Feb 2008
Posts: 30
#8: Mar 12 '08

re: How does Rnd() work? How to store a value.


Thanks for your help so far.
I'll try to get some others interested too by posting the problem again in its present state
Scott Price's Avatar
Moderator
 
Join Date: Jul 2007
Location: Seattle, WA
Posts: 1,314
#9: Mar 12 '08

re: How does Rnd() work? How to store a value.


Bumping your thread by adding another post to the end of it is usually sufficient if you aren't getting any results. Please refrain in the future from double posting.

Here is the Access help file for the Rnd() function. To get this help file yourself, simply position the cursor within the word you want help on and press F1.

Quote:
Rnd Function


Returns a Single containing a random number.

Syntax

Rnd[(number)]

The optional number argument is a Single or any valid numeric expression.

Return Values

If number is Rnd generates
Less than zero The same number every time, using number as the seed.
Greater than zero The next random number in the sequence.
Equal to zero The most recently generated number.
Not supplied The next random number in the sequence.



Remarks

The Rnd function returns a value less than 1 but greater than or equal to zero.

The value of number determines how Rnd generates a random number:

For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence.

Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.

To produce random integers in a given range, use this formula:

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

Here, upperbound is the highest number in the range, and lowerbound is the lowest number in the range.

Note To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.
Regards,
Scott
Reply


Similar Microsoft Access / VBA bytes