VBA code to pull X number of records from a database

knarlyd@hotmail.com

Registered User.
Local time
Today, 09:05
Joined
Sep 6, 2013
Messages
43
I need some help with VBA code to first generate a random number between 1-4.

Then I would like to use that number to pull that many records out of the database; the records pulled can be random, or whatever.

For example, we have 4 people and my goal is to choose a random # of records to pull for each person. So, for person #1, it might only pull 1 record, then for person #3, it may pull 2 records, etc.
Ideally, it wouldn't pull the same # twice when run for that "session".

After this, I'm hoping to generate all those results into emails (eg for person 3 it would generate 3 emails, etc)

Currently it's all done manually and I think it's doable with Access.
I have some code already for generating "1" email message, but do not know how to pull X number of emails all at once. (X is the random #).

A LOT to ask I'm guessing for coding.
Any help in one or more areas would be greatly appreciate!
 
Last edited:
Here's a function I use to generate a random number with a given max:

Code:
Public Function GetRandom(intMax As Integer)
  Randomize    ' Initialize random-number generator.
  GetRandom = Int((intMax - 1 + 1) * Rnd + 1)   'Generate random value between 1 and input.
End Function
 
I placed your code in a new module "RandNumber" however I'm not sure how to use it quite frankly....
:(
 

Users who are viewing this thread

Back
Top Bottom