I would like to create a form that generates a random 6 digit number when creating a new record and then use this number field as the primary key. Only thing is I'm not too good at the coding. Can anyone help me?
hopefully with only 30,000 records you wont get too many clashes so the loop wont have to keep checking
Code:
Dim i As Long
Randomize ' Initialize random-number generator.
i = Int((900000 + 1) * Rnd + 100000)
Do While DCount("ID", "MyTable", "ID = " & i)
i = Int((900000) * Rnd + 100000)
Loop
MyField = i
Private Sub Serialtxt_BeforeUpdate(Cancel As Integer)
Dim i As Long
Randomize ' Initialize random-number generator.
i = Int((900000 + 1) * Rnd + 100000)
Do While DCount("ID", "Check Requests", "ID = " & i) ' "Check Requests" is the table name.
i = Int((900000) * Rnd + 100000)
Loop
SERIAL = i ' SERIAL is the field name listed in the table "Check Requests"
End Sub
I attached this code to a text box on the Form. Everytime I create a new record the value in the textbox returns blank both on the form and in the table.