|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB Databases Basics 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
|
|
|
June 4th, 2005, 05:26 PM
|
Authorized User
|
|
Join Date: Jun 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Random Table/Field/Record
(Please Forgive cross-post)
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)
|
June 7th, 2005, 10:32 AM
|
Authorized User
|
|
Join Date: Jun 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I can only assume that noone out there knew how to answer this, and since I finally figured it out figured I'd share it.
Private Cmd_Generate
'This is the button for generating results from the database
dim intID
dim strPrefix
dim strSuffix
intID((Rnd*50)+1)
'Where 50 is the number of records in the Prefix Field
adodc1.recordsource= "Select ID, Prefix FROM Hungarianmale WHERE ID=" & intRnd
adodc1.refresh
'Updates all associated textboxes using adodc1 as a datasource.
strPrefix = label1.caption
'label1 is using adodc1 as a datasource, hence, the variable is now loaded in there
intID((Rnd*46)+1)
'Where 50 is the number of records in the Suffix Field
adodc1.recordsource= "Select ID, Suffix FROM Hungarianmale WHERE ID=" & intRnd
adodc1.refresh
'Updates all associated textboxes using adodc1 as a datasource.
label1.datafield=Suffix
'Makes Label1 reference the suffix field in the database.
strSuffix=label1.caption
text1.text=strPrefix + strSuffix
'This is where the final output is seen by the end user.
End
|
June 7th, 2005, 11:22 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
hi there..
dont forget to seed the random function b/c if you dont do that the rnd numbers will always be the same...
HTH
Gonzalo
|
June 7th, 2005, 11:38 AM
|
Authorized User
|
|
Join Date: Jun 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Alright, I'll claim ignorance, I forget how to do that. (I used to know, or thought I did. :) )
|
June 7th, 2005, 11:43 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
Lol.. no problem..
Randomize Statement
Initializes the random-number generator.
Syntax
Randomize [number]
The optional numberargument is aVariant or any validnumeric expression.
Remarks
Randomize uses number to initialize the Rnd function's random-number generator, giving it a newseed value. If you omit number, the value returned by the system timer is used as the new seed value.
If Randomize is not used, the Rnd function (with no arguments) uses the same number as a seed the first time it is called, and thereafter uses the last generated number as a seed value.
Note To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.
Just call it at the beginning of your program.. seed it with milliseconds or something like that.
HTH
Gonzalo
|
June 7th, 2005, 06:56 PM
|
Authorized User
|
|
Join Date: Jun 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
*hrmms* Some of these questions are pretty basic I know... But.. How would you get something like Randomize [Number] to activate on boot? Everytime I try to get it to do that, it complains about being outside of a procedure.
that make any sense?
|
June 8th, 2005, 08:19 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
how you start your program?? with a form?? with a main sub??
just put it where ever you start your program...
if you are using a form, you can put it in the load of the form...
HTH
Gonzalo
|
June 8th, 2005, 10:30 AM
|
Authorized User
|
|
Join Date: Jun 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
*laughs* Right.. Sorry, I've been out of touch with VB for about 2 years now, and just started working on a project again recently. Lots of little stuff still hangin' out in the 'fuzzy space' part of my brain.
My next question is bound to be about data exporting and creating databases from new information.
Now I haven't quite gotten this far yet, but when I ultimately compile my program, I'm assuming the associated DB's will compile into it?
|
June 8th, 2005, 10:34 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
no.. wrong.. the db is other file but will not go in the exe...
you can send it in the deploy package, but will not compile nor be inside your vb project...
HTH
Gonzalo
|
June 8th, 2005, 10:35 AM
|
Authorized User
|
|
Join Date: Jun 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
*mutters* Huh... Ah well. :)
|
|
|