Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 17th, 2008, 07:54 AM
Authorized User
Points: 135, Level: 2
Points: 135, Level: 2 Points: 135, Level: 2 Points: 135, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2007
Location: Dallas, TX, .
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?

Reply With Quote
  #2 (permalink)  
Old April 17th, 2008, 10:32 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
  #3 (permalink)  
Old April 17th, 2008, 10:33 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:26 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.