Wrox Programmer Forums
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 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 June 4th, 2005, 05:24 PM
Authorized User
 
Join Date: Jun 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Random Result from Database

For those of you who read the previous post:
I've moved forward a bit, and now need what *should be* a relatively simple question answered.

How do I request a specific record from a particular field randomly?

I.E. Say I wish to pull a random record from table A, Field 1, where field 1 has 50 records.

I thought perhaps utilizing the Int(Rnd * #)+1 process would work for this, (Where the Int is used to pull a record by ID#/Field) but as it turns out, I can't seem to find any reference material to tell me what I need to use to accomplish this. Does anyone of you folks have an idea?

(Further - If I perhaps wanted to choose a random record from a random field, what process would I use to do this? (Say I wanted to pull a random record 1-50, from random Field 1-3, on Table "Example")

(Further further - From Random Table A-C?)

Any help would be appreciated. (I now understand the basics of database accessing)

 
Old June 24th, 2005, 05:01 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

You could put the records into an array (there is a recordset function for this), and use a random number—limited to the array bounds—to select the record out of the array.

Additionally, having done this, the array is multi-dimensional, having an element per field (column). You can perform the same operation to randomly select amongst the array of fields which is to be found at each 'node' of the array of records.
 
Old June 27th, 2005, 02:46 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

"A random record from a random field"? Sorry, I do not get it. A "record" is a collections of "fields", and not viceversa.
So you have a table "Example". Open the table and get the total number of records. Generate a random number as Fix(rnd * NumberOfRecords). Move to that record (how to do it depends on the database) and read it.
If you need to extract a random field from that record, do the same using the recordSet.Fields collection
To Brian: your suggestion is not feasible for tables with lots and lots of records, you cannot just put all of them in a memory array.
Marco





Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP - Random values from database cancer10 Classic ASP Databases 1 December 21st, 2006 01:24 PM
display random record from access database jialin PHP Databases 1 December 2nd, 2005 09:18 PM
random numbers from/to database HammR PHP Databases 10 January 14th, 2005 12:08 PM
Generating 5 unique random records from a database ps124 ASP.NET 1.0 and 1.1 Basics 2 March 8th, 2004 05:59 PM
random records from database HammR PHP Databases 1 December 11th, 2003 02:03 PM





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