Create button to generate Random Number

M

MackBlale

I want to create a button GenerateID control on my form that when pressed
will use the following expression Int((10000-1+1)*Rnd()+1) to generate a
random number and display the result in a text box.

I want to assign this number to tblEmployees in a field ClientID.

If the number already exists, I would like an error message "Generate
Again!", or automatically rerun the expression until a unique number is found.

There will never be more than 1,000 Client IDs.
 
C

Cheese_whiz

Hi McBlal:

I'd try something like this. It's untested:

YerButton_click()
Dim intMyRanID as Integer

Do intMyRanID = Int ((10000 - 1 + 1) * Rnd + 1)
Me.YourTextBoxName = intMyRanID

Loop While DCount("ClientID", "YourTable", "ClientID = " & intMyRanID)>0

End Sub

HTH (and works!),
CW
 
M

MackBlale

CW,
Thanks a lot. This is code that is working for me right now:

Private Sub GenerateNum_Click()
Dim intMyRanID As Integer
Do
intMyRanID = Int((10000 - 1 + 1) * Rnd() + 1)
Me.LNA Number = intMyRanID
Loop While DCount("ClientID", "tblEmployees", "ClientID=" & intMyRanID) > 0
End Sub

I am using a tabbed form that displays the ClientID on the first tab. Now,
I want to store the generated number in the ClientID field when the user
presses the save record button. I also want the ClientID field on the tabs
to display the ClientID number. Finally, I need to disable or hide the
GenerateID button if the user is viewing a record that has already been
assigned a ClientID.

Thanks.
Mack
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top