Keep random numbers for multiple records

Vivirtruvian

Registered User.
Local time
Today, 10:30
Joined
Jun 20, 2017
Messages
19
[SOLVED] Keep random numbers for multiple records

Hi all,

I am using a form to allow operators to scan barcodes one-by-one into our database. The operator will initially enter some text information which will be attached to the barcode's record in the table.

I am successfully using the AfterUpdate DefaultValue code to keep information stored in the relevant text boxes every time a record is updated in the main table.

Where I am running into trouble is with a randomly generated number that I need to attach to the barcodes also. Creating the number is not an issue, however I need the number that is generated on the initial opening of the form to stay the same for every scan until the form is closed again. In other words, I only want the number to be generated on opening, and not every time the form creates a new record. Unfortunately using the Defaultvalue code has no impact on keeping the random number from changing.

I understand that the random number function is there to create a random number on a record-to-record basis, but just wondering if there is any way to cheat the system a little bit?

Thanks in advance!
 
Last edited:
suggest to put the generated random number in Tempvars, then use tempvars as your default value, ie:

Private Sub Form_Open(Cancel As Integer)
If IsNull(Tempvars("random_bar")) Then
Tempvars.Add "random_bar", 0
Tempvars("random_bar")= yourfunctionToGenerateRandomNumber()
End If
End Sub

Private Sub Form_Close()
Tempvars.Remove "random_bar"
End Sub

now on your Textbox Default Value put this:

Default Value: =Tempvars("random_bar")
 
if your uncomfortable of using Tempvar, another alternative is to have an unbound textbox (set its Visible Property to False so it will be hidden) in a form (say txtRandom), then on the Form's Load Event set its value to the random number generator you have. make the default value of your bound textbox to txtRandom:

Private Sub Form_Load()
Me.txtRandom = randomgenerator()
End Sub


on your bound textbox:

Default Value: =[txtRandom]
 
Hi arnelgp,

I tried your first suggestion and it worked an absolute treat! Thank you so much for the quick response!
 

Users who are viewing this thread

Back
Top Bottom