Hi
I an auditor and use the following code in one of my sampling databases.
Private Sub Command2_Click()
Dim strsql
Dim TopN
Set db = CurrentDb
Set qdf = db.QueryDefs("qry sample")
TopN = InputBox("How many samples?")
strsql = " Select Top " & TopN & " [qry Invoices (no threshold)].ID, [qry Invoices (no threshold)].Type, [qry Invoices (no threshold)].Date, [qry Invoices (no threshold)].[Account Code], [qry Invoices (no threshold)].Year, [qry Invoices (no threshold)].Supplier, [qry Invoices (no threshold)].[Reference 3], [qry Invoices (no threshold)].Reference, [qry Invoices (no threshold)].Period, [qry Invoices (no threshold)].Gross, [qry Invoices (no threshold)].Random, [qry Invoices (no threshold)].From, [qry Invoices (no threshold)].To FROM [qry Invoices (no threshold)]WHERE ((([qry Invoices (no threshold)].Gross) Between [Lower Limit] And [Upper Limit] Or ([qry Invoices (no threshold)].Gross) Between -[Lower Limit] And -[Upper Limit]))ORDER BY [qry Invoices (no threshold)].Random;"
qdf.SQL = strsql
Set qdf = Nothing
Set db = Nothing
DoCmd.OpenQuery "qry sample"
End Sub [/color red]
The population is in a query [qry Invoices (no threshold)]. This query also has a [random] field which is a randomly generated number, which changes each time any command is run. The code below brings up a dialog box asking how many samples you want to take (TopN), and then creates a query ([qry sample]) which sorts the data by the random number field and displays the top (TopN) number of records.
Hence a user definable random sample.
The code can also be amended to give a percentage of the total population. Insert the word "percent" (no speech marks) after strsql = " Select Top " & TopN & " [/color red] in the code above.
Hope this helps.
AL