View Full Version : Random Number Generator


velcrowe
06-01-2006, 07:02 AM
I would like to create a simple access program to assign locker numbers between 1 and 750 to employees. Whenever I want to give an employee a new locker number, I would like to click a button to assign them a new number and then enter the date of the assignment. Is this possible and simple?:rolleyes:

MStCyr
06-01-2006, 07:08 AM
Check out the Rnd() function

velcrowe
06-01-2006, 07:15 AM
Thanks. To edit my last question. I have 100 persons who have lockers. I want to randomly pick five men and five women for locker inspections. Would the Rnd() function apply these sets of data? Can I randomly select five non-repeating men or women?

MStCyr
06-01-2006, 07:49 AM
The following function will do that for you..


Function FindRandom (RecordSetName As String, Fieldname As String)
Dim MyDB As Database
Dim MyRS As Recordset
Dim SpecificRecord As Long, i As Long, NumOfRecords As Long

Set MyDB = CurrentDB()
Set MyRS = MyDB.OpenRecordset(RecordSetName, DB_Open_Dynaset)
On Error GoTo NoRecords
MyRS.MoveLast
NumOfRecords = MyRS.RecordCount
SpecificRecord = Int(NumOfRecords * Rnd)
If SpecificRecord = NumOfRecords Then
SpecificRecord = SpecificRecord - 1
End If
MyRS.MoveFirst
For i = 1 To SpecificRecord
MyRS.MoveNext
Next i
FindRandom = MyRS(Fieldname)

Exit Function
NoRecords:
If Err = 3021 Then
MsgBox "There Are No Records In The Dynaset", 16, "Error"
Else
MsgBox "Error - " & Err & Chr$(13) & Chr$(10) & Error, _
16, "Error"
End If
FindRandom = "No Records"
Exit Function
End Function

velcrowe
06-01-2006, 08:07 AM
I am using only three fields...locker number, name, and date(to keep track of query run date) on my table, query, and form.

Should I place this code in the General Declaration of the form and do I need a requer command button to run on instance from the form?

MStCyr
06-01-2006, 11:43 AM
yes, this code definitely goes into a module