Rnd Function and Validation Check on Form (1 Viewer)

wilderfan

Registered User.
Local time
Today, 00:09
Joined
Mar 3, 2008
Messages
172
I'm trying to generate a random number for a text box and then check to ensure that the generated number does not already exist in the table.

I have attached a very small file (using ACCESS 2007). The range of generated numbers is very small (1 to 8) to keep the test manageable.

I used the Rnd function in the GotFocus event and then placed the validation check in the BeforeUpdate event. Not sure is this is the proper way to do this.

The index property for the field is set to No in the table design. (I tried setting it to Yes, no duplicates; but that didn't help and led to more problems.)

Long story short: I'm getting duplicates.

I'm hoping to bounce back to the text box and generate another random number automatically. Maybe I need to work in a loop of some kind here.

Any suggestions?
 

Attachments

  • RandomTest.zip
    14.9 KB · Views: 72

Mr. B

"Doctor Access"
Local time
Today, 02:09
Joined
May 20, 2009
Messages
1,932
Check the code in the OnGotFocus event of you control in the attached file. See if this does what you want.
 

Attachments

  • RandomTest.zip
    24.3 KB · Views: 76

bbijah0424

New member
Local time
Today, 09:09
Joined
Aug 19, 2011
Messages
1
How would you generate all 8 nos randomly in just one record with no duplicates at a time? Thanks.
 

Users who are viewing this thread

Top Bottom