Results 1 to 13 of 13
  1. #1
    Soupy8728 is offline Novice
    Windows 11 Access 2016
    Join Date
    Mar 2024
    Posts
    18

    Random records based off of number of records

    Good morning all,

    Here is what I'm trying to do:

    Table has over 100,000 records. Within the table there is a field called Department and it contains various departments (Department A, Department B...etc). I need to run a random sample based off the number of records each Department has. For example, if Department A has more that 100 records but less than 1000 records, I need a random sample of 35 records. If Department B has more than 100 records but less than 1000 records, I need a random sample of 35 records. I ran a query to count the number of records each department has but I just can't figure out how to randomly select only X amount of records based off those numbers. I hope this makes sense. Thank you in advance for your time.

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,824
    Something like this perhaps?
    https://answers.microsoft.com/en-us/...b-102e151a500f
    Note that you require an autonumber (or other number field that is unique and probably consecutive).
    I suspect there are other logical considerations that you'll need to code for, so that this is done according to decision making. F'rinstance you have not said what happens if there are more than 1000 records for a department. In that case, do nothing? Also, I imagine this requires a loop to iterate over all the departments.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,743

  4. #4
    Soupy8728 is offline Novice
    Windows 11 Access 2016
    Join Date
    Mar 2024
    Posts
    18
    Quote Originally Posted by Micron View Post
    Something like this perhaps?
    https://answers.microsoft.com/en-us/...b-102e151a500f
    Note that you require an autonumber (or other number field that is unique and probably consecutive).
    I suspect there are other logical considerations that you'll need to code for, so that this is done according to decision making. F'rinstance you have not said what happens if there are more than 1000 records for a department. In that case, do nothing? Also, I imagine this requires a loop to iterate over all the departments.
    Thank you. I have unique numbers so that's not an issue. There are other logical considerations as you have stated which I believe I will be able to address once I get this initial problem figured out. In my query where I counted the records, I created another calculation field to give me my "Sample size". For instance, if the count of records for Department A was between 100 and 1000, then it assigned the Sample Size of 35. Now what I need to do is figure out how to tell access to take that "Sample size" number and randomly select that amount of records from all of Department A records. Additionally, that "Sample size" number can vary so if they have less that 100 records, it will be 15, if they have more than 1000 records it will be 75. Hope that helps.

  5. #5
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,479
    Just thinking of the looping, you will need to do 2 loops, one for the Depts and one for the number of records for each Dept. Once you know the number of records you need for a Dept, you would do a loop though those Dept records to select a random record and write it to another table(or you can add a field to your table for this process to mark the selected records and just blank that field out each time you run this process), then update your counter to x=x+1, compare it to your total needed and keep going until you get the total needed, then loop again for the next department. Also each time you select a random record, you need to check (Dlookup will work) to make sure you did not already select that record and if you did, loop back to get another record.

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,824
    I say 1 loop for department only. Calculate the number of records per dept and use that in a Select query statement with Top N predicate (sub query).
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    Soupy8728 is offline Novice
    Windows 11 Access 2016
    Join Date
    Mar 2024
    Posts
    18
    Quote Originally Posted by Micron View Post
    I say 1 loop for department only. Calculate the number of records per dept and use that in a Select query statement with Top N predicate (sub query).
    Thank you but I'm not sure how to do that. I was hoping it would be a lot easier for my experience level. Hahaha...

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,016
    Take it one step at a time.
    Break it down into small steps, and work on each in turn.

    I would start with
    Code to print random records with a hardcoded number of times, but still using a variable.
    I would probably store the recordID selected in a table, so that it could not be selected more than once. You could then use that table to produce all your records as the source.

    When you have that working you need to surround that code with a loop to process each record in turn from a recordset. That recordset would also hold the number of record to produce (which you say you already have).

    Not sure how you are going to get truly random records though.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    Soupy8728 is offline Novice
    Windows 11 Access 2016
    Join Date
    Mar 2024
    Posts
    18
    Quote Originally Posted by Welshgasman View Post
    Take it one step at a time.
    Break it down into small steps, and work on each in turn.

    I would start with
    Code to print random records with a hardcoded number of times, but still using a variable.
    I would probably store the recordID selected in a table, so that it could not be selected more than once. You could then use that table to produce all your records as the source.

    When you have that working you need to surround that code with a loop to process each record in turn from a recordset. That recordset would also hold the number of record to produce (which you say you already have).

    Not sure how you are going to get truly random records though.
    Thank you for your patience and your guidance. I was able to generate the random records part fairly easy but only with a hard code number such as Top 100 or Top 10%. I was trying to see if there was a way to use my record count query to populate that hard code number automatically as my records change on a daily basis.

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,743
    Soupy8728,

    Did you look at my sample random records and selection mentioned in post #3?

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,016
    Quote Originally Posted by Soupy8728 View Post
    Thank you for your patience and your guidance. I was able to generate the random records part fairly easy but only with a hard code number such as Top 100 or Top 10%. I was trying to see if there was a way to use my record count query to populate that hard code number automatically as my records change on a daily basis.
    Not really random then?
    You would build your sql with the value coming from your source query as a recordset.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    Soupy8728 is offline Novice
    Windows 11 Access 2016
    Join Date
    Mar 2024
    Posts
    18
    Quote Originally Posted by orange View Post
    Soupy8728,

    Did you look at my sample random records and selection mentioned in post #3?
    Yes but that is beyond my capability. Thank you tho.

  13. #13
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,824
    We don't know what your experience level is with this and there isn't enough info to create anything. What are you expecting from us, exactly? Guidance or a tangible solution, or something else? Is posting a zipped db copy an option?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2022, 07:08 AM
  2. Replies: 2
    Last Post: 03-15-2022, 09:31 AM
  3. Replies: 17
    Last Post: 12-08-2021, 11:21 PM
  4. Query Random records based on criteria
    By Grant Shea in forum Access
    Replies: 31
    Last Post: 09-09-2016, 03:10 PM
  5. Select Random Records Based on Conditions
    By access123 in forum Queries
    Replies: 1
    Last Post: 10-27-2010, 10:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums