Private Sub RandomBtn_Click()
Dim vRandomSelect As Long
Dim vTableCount As Long
Dim vRecordsToFlag As Long
Dim vTableName As String
Dim vIndexFieldName As String
Dim vFlagFieldName As String
Dim vStartRecCounter As Long
Dim I As Long
Dim MyDB As Database
Dim MyRS As Recordset
Set MyDB = CurrentDb
vTableName = InputBox("Enter the name of the table to be random sampled.", , "tblCases")
vIndexFieldName = InputBox("Enter the name of the Autonumber field", , "RecCounter")
vFlagFieldName = InputBox("Enter the name of the Yes/No field to be flagged", , "RandomSelection")
vStartRecCounter = DMin(vIndexFieldName, "tblCases", vIndexFieldName & " > 0")
vTableCount = DCount("*", vTableName)
Set MyRS = MyDB.OpenRecordset(vTableName, dbOpenDynaset)
vRecordsToFlag = CLng(InputBox("How many records are to be randomly selected?", , vTableCount))
Randomize
For I = 1 To vRecordsToFlag Step 1
Random:
vRandomSelect = CLng(Int(Abs((vTableCount * Rnd) + 1))) + vStartRecCounter
MyRS.FindFirst vIndexFieldName & " = " & vRandomSelect
If Not (MyRS.NoMatch) Then
MyRS.Edit
If MyRS(vFlagFieldName) = False Then
MyRS(vFlagFieldName) = True
MyRS.Update
Else
GoTo Random
End If
Else
GoTo Random
End If
Next I
End Sub