Results 1 to 3 of 3
  1. #1
    Join Date
    May 2003
    Location
    Bucharest
    Posts
    16

    Unanswered: extract records randomly from a table by criteria

    how can i extract fix number of records, randomly from a table by a specify criteria?
    Theodor

  2. #2
    Join Date
    Dec 2002
    Posts
    60
    One way would be to create a subset of records based on criteria (RecordSet object). Once you know how many records are in subset (RecordCount property), you use Randomize and Rnd functions to pull out random records (look at help).

  3. #3
    Join Date
    May 2003
    Location
    Dallas
    Posts
    817
    Provided Answers: 5
    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, 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(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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •