Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 17th, 2008, 07:54 AM
Authorized User
 
Join Date: Feb 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default Rnd Function

I'm trying to use the Rnd function to select a certain number of records from a table. Let's say I have 600 records in tblMain and I need to review 50 of these. How would I go about selecting 50 of these at random?

 
Old April 17th, 2008, 10:32 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

You would need to do something like this:

As far as getting a random number, I would refer to the AutoNumber field, which I am assuming is there.

Then take the MAX() value of the ID field as the upper bound of the random number generator. Use 1 for the lower bound.

Then using a target table to store the individual records as they are pulled (with a delete query to delete all the data before each run), do something like this (psuedo code):

--------------------
Using an input box, take the number of records in the sample (iCount)
iCount = InputBox("Enter a quantity of records to view")
Let's say 50 is entered

Take the highest ID field number (iMax)

iScope is your random number that will call the ID field

Randomize
Do Until i = iCount + 1
iScope = Int((1 * Rnd) + iMax)

   On Error Resume Next
   Err.Clear
   sSQL = "SELECT * FROM tblMain WHERE [IDField] = " & iScope
   open recordset
   If there is an error, then skip
   If Err = 0 Then
     Move the record to the target table
   End If
   i = i + 1
Loop

Then open the resulting table with a form or report.
-------------------------------


Did that help?
This should work. Let me know if iScope keeps returning the same initial number, which it shouldn't do.

mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old April 17th, 2008, 10:33 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Critical Change:

Change this section:
If Err = 0 Then
     Move the record to the target table
   End If
   i = i + 1

To this:
If Err = 0 Then
     Move the record to the target table
     i = i + 1
   End If

Sorry.


mmcdonal

Look it up at: http://wrox.books24x7.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
send variable in function to another function schoolBoy Javascript How-To 6 March 3rd, 2007 09:16 AM
Not a function ... why? nloding Javascript 0 February 20th, 2007 07:19 PM
problem with Rnd() kanoorani Beginning VB 6 3 April 12th, 2006 09:38 AM
How to call javascript function from VB function vinod_yadav1919 VB How-To 0 February 13th, 2006 06:03 AM
retreive function/Line from macro or function? MikoMax J2EE 0 April 1st, 2004 04:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.