random selection not marking enough

pbuethe

Returning User
Local time
Today, 11:10
Joined
Apr 9, 2002
Messages
210
I am trying to mark a random set of records in the table. The user enters the percentage of records to mark (txtPercentage). e.g. if the user wants to mark 45% of the records (s)he would enter 45 on the form. There are other criteria for selecting records also. It seems to work except that it is not selecting enough records.
e.g. if trying to mark 45% of 37 records, it should mark 17 records, but it is only marking 14.

My code is as follows:

Code:
Sub MarkTitleI(stAUID As String, stSample As String, stRType As String, stLType As String, stQuery As String)
    Dim R As Recordset
    Dim Q As QueryDef
    Dim Top, Bottom, K, Max
    
    Set Q = dbs.QueryDefs(stQuery)
    Q.Parameters("pAUID") = stAUID
    Q.Parameters("pSample") = stSample
    Q.Parameters("pRevType") = stRType
    Q.Parameters("pListType") = stLType
    Set R = Q.OpenRecordset()
    R.MoveLast: R.MoveFirst   'set recordcount
    
    Top = 0
    Bottom = R.RecordCount - 1
    Max = R.RecordCount

    For K = 1 To Int(Max * 0.01 * Forms!frmTitleIEntry!txtPercentage)
        R.MoveFirst
        'move to a random record
        R.Move Int((Top - Bottom + 1) * Rnd + Bottom)
        R.Edit
        R.Fields("Contract") = "Title I"
        R.Update
        R.Requery  'remove all non-zero records
        Bottom = Bottom - 1  'adjust random range
    Next K
    R.Close
    
End Sub

Before calling this sub I execute a query which clears the Contract field, and then call Randomize. Then the sub is called several times using different queries as stQuery.

Thanks as always for your help.
 
p,

I'd say that you are choosing the same ones multiple times. You should see
whether or not it is checked when you select it. You could requery the
table, grabbing only the unchecked ones.

Wayne
 
Wayne,

I tried your suggestion and got closer to the number that should have been marked. Then I figured out that the reason it is a little off may be because I am selecting males, females, and "eliminated" separately. My supervisor said it is OK, it is close enough. Thanks for your help!
 
Last edited:

Users who are viewing this thread

Back
Top Bottom