473,499 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to assign incrementing numbers for range and random sample that range?

14 New Member
looking for MS Access VBA code to do the following: I am trying to assign incrementing numbers ( ie - 1-10) to records in an existing table by date and pull a 10% random sample from that table based on the numbers assigned. Any help is appreciated. Existing Example Table:

number date person
45001 1/19/2011 Ray
21020 1/19/2011 Steve
49009 1/19/2011 Bob
72005 1/19/2011 Ray
62203 1/19/2011 Bob
83406 1/19/2011 Ray
88700 1/19/2011 Tom
99174 1/19/2011 Tom
11726 1/19/2011 Tom
90999 1/19/2011 Ray
81119 1/18/2011 Ray
12226 1/18/2011 Steve
23334 1/18/2011 Bob
14448 1/18/2011 Ray
65550 1/18/2011 Bob
96663 1/18/2011 Ray
77771 1/18/2011 Tom
58882 1/18/2011 Tom
39994 1/18/2011 Tom
80996 1/18/2011 Ray

i want it to add the new field (No) to the existing table assigning numbers incrementing from 1 to x where x is the count of records for each varying date, like such

No number date person
1 45001 1/19/2011 Ray
2 21020 1/19/2011 Steve
3 49009 1/19/2011 Bob
4 72005 1/19/2011 Ray
5 62203 1/19/2011 Bob
6 83406 1/19/2011 Ray
7 88700 1/19/2011 Tom
8 99174 1/19/2011 Tom
9 11726 1/19/2011 Tom
10 90999 1/19/2011 Ray
1 81119 1/18/2011 Ray
2 12226 1/18/2011 Steve
3 23334 1/18/2011 Bob
4 14448 1/18/2011 Ray
5 65550 1/18/2011 Bob
6 96663 1/18/2011 Ray
7 77771 1/18/2011 Tom
8 58882 1/18/2011 Tom
9 39994 1/18/2011 Tom
10 80996 1/18/2011 Ray

then i want it to create/return in a new table the random sample (10%) for each date change - example output would be a new table like such:

No number date person
5 62203 1/19/2011 Bob
2 12226 1/18/2011 Steve


if i changed the % to 20% random sample- I want a table like such:

No number date person
9 11726 1/19/2011 Tom
7 88700 1/19/2011 Tom
10 80996 1/18/2011 Ray
2 12226 1/18/2011 Steve

and so on - thanks for helping
Jan 19 '11 #1
2 2368
Rabbit
12,516 Recognized Expert Moderator MVP
You could use ranking subquery to get the 1-10, if it's necessary. If the ranking is only for the purpose of selecting a random sample, then you don't need it. If it's needed though for a different purpose, then there's plenty of threads on these forums about ranking records.

As for the random sample, you could use an update query that sets a field using Rnd() seeded with a unique ID and time. Then you could use a query that will select from those records using a subquery. The subquery would return the top # of IDs sorted by the randomized column.
Jan 19 '11 #2
ADezii
8,834 Recognized Expert Expert
You can easily create a Query that will generate a Random Number of Records, as a Percentage of Total Records, for a given Date.
  1. Create a Calculated Field called [Random]. This Field will call a Public Function, and pass to it the [Number] Field. You will also sort on this Calculated Field (Ascending). The [Number] Field will not be used within the Function, but is required.
  2. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fGenerateRndNum(sngDummy As Long) As Single
    2. 'The Parameter sngDummy is required but not used
    3. Randomize
    4.  
    5. fGenerateRndNum = Rnd()
    6. End Function
  3. Create a Query consisting of the [Number], [Date], [Person], and [Random] Fields. Hide the Random Field since it has already done its job, and is no longer needed.
  4. Set the Top Values Property of the Query to 20%.
  5. Set the Date Criteria to #1/19/2011#.
  6. Query Definition:
    Expand|Select|Wrap|Line Numbers
    1. SELECT TOP 20 PERCENT tblTest.Number, tblTest.Date, tblTest.Person
    2. FROM tblTest
    3. WHERE tblTest.Date=#1/19/2011#
    4. ORDER BY fGenerateRndNum([Number]);
  7. Execute the Query.
  8. Sample results from three Trial Runs.
    Expand|Select|Wrap|Line Numbers
    1. Number   Date          Person
    2. 99174    1 /19/2011    Tom
    3. 62003    1 /19/2011    Bob
    Expand|Select|Wrap|Line Numbers
    1. Number   Date          Person
    2. 72005    1 /19/2011    Ray
    3. 99174    1 /19/2011    Tom
    Expand|Select|Wrap|Line Numbers
    1. Number   Date          Person
    2. 49009    1 /19/2011    Bob
    3. 72005    1 /19/2011    Ray
  9. Youo can now convert the Query to a Make Table Query, and execute it once for each Unique Date.
Jan 19 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
13560
by: Rahul Anand | last post by:
Hi all, I am trying to fetch 5 records, randomly picked and in random order from a MySQL table (MySQL version > 3.23). I wrote my SQL Query as SELECT name,id FROM tablename WHERE active =...
9
2248
by: Bart Nessux | last post by:
I am using method 'a' below to pick 25 names from a pool of 225. A co-worker is using method 'b' by running it 25 times and throwing out the winning name (names are associated with numbers) after...
14
11993
by: amywolfie | last post by:
Hi All: I know this is simple, but I just can't seem to get there: I need to sort a table by a text field (txtDescription), then assign sequential numbers to the field SEQUENCE in table. ...
1
2313
by: BerkshireGuy | last post by:
Hello everyone, I had a query that took 3,000 random records (which are addresses)from a table. This recordset is to send off surveys. I've noticed by looking at the data, that I have some...
5
2256
by: jordi | last post by:
I need the random.sample functionality where the population grows up to long int items. Do you know how could I get this same functionality in another way? thanks in advance. Jordi
7
9768
by: noob92 | last post by:
I'm using python to write a program that, when given a list of random numbers, sorts them from lowest to highest then it asks the user for a number and it checks that number's place in the sorted...
1
1560
by: lilsn0opy04 | last post by:
I am pretty new to Python. I wanted to create a program so that if a user entered a number, then that amount of random numbers will appear. I wanted my random.randrange(100)+1 "How many numbers...
1
1866
by: liscete | last post by:
I need to create a simple random sample of 20,000 out of 4,000,000. what is the statement? I was told select top 20K would do this but i believe the select top XXX is based on a first ordered...
2
1605
by: twomikey | last post by:
I am confused and not even sure this can work. I have a database that needs to assign licenses that we bought for IP addresses. We have purchased 1000 of these and they are in numeric order. How...
2
6431
by: Geog | last post by:
Hello, I'd like to know if it is possible to make a random sample of a number/percentage of rows with the same value, i.e. to select randomly 6000 registers of field "x" where x=0 Thank you
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7012
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7225
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4920
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.