ausmoran
Sep 8th, 2000, 08:52 AM
I am still struggling with this and need a little more help, I'm afraid. I am attempting to select a random record from an Access97 table in which I have approximately 20,000 records. We are holding a large-scale raffle and do not want to use raffle tickets...but will, instead, display a form on a large screen in an auditorium. When the host clicks on a command button, I would like to have a twenty-five or thirty second pause (with names from the table scrolling on the form) and then select a random winner.
the code I have been playing with is listed here (from the MS Knowledge Base), but because I am such a novice, I am not even sure where to place it or how to execute it...I need your help!
Thanks in advance.
Option Compare Database
Option Explicit
Function FindRandom(Final_Ticket_datafile As String, Custom_1 As String)
Dim MyDB As Database
Dim MyRS As Recordset
Dim SpecificRecord As Long, NumOfRecords As Long
Set MyDB = CurrentDb()
Set MyRS = MyDB.OpenRecordset(Final_Ticket_datafile, dbOpenDynaset)
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(Custom_1)
Exit Function
NoRecords:
If Err = 3021 Then
MsgBox "Error - " & Err & Chr$(13) & Chr$(10) & Error, 16, "Error"
End If
FindRandom = "No Records"
Exit Function
End Function
the code I have been playing with is listed here (from the MS Knowledge Base), but because I am such a novice, I am not even sure where to place it or how to execute it...I need your help!
Thanks in advance.
Option Compare Database
Option Explicit
Function FindRandom(Final_Ticket_datafile As String, Custom_1 As String)
Dim MyDB As Database
Dim MyRS As Recordset
Dim SpecificRecord As Long, NumOfRecords As Long
Set MyDB = CurrentDb()
Set MyRS = MyDB.OpenRecordset(Final_Ticket_datafile, dbOpenDynaset)
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(Custom_1)
Exit Function
NoRecords:
If Err = 3021 Then
MsgBox "Error - " & Err & Chr$(13) & Chr$(10) & Error, 16, "Error"
End If
FindRandom = "No Records"
Exit Function
End Function