473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Combine two tables randomly?

Ok, let's try this....

I've got two tables: tblSuffolk, tblNassau.

Both tables have the same EXACT fields:

empUID - self explanatory
empFirstName - self explanatory
empLastName - self explanatory
empGender - self explanatory
empCounty - county where employee works
empSelected - value entered after employee has been selected for a group
empGroupNumber - container for automatically assigned group number once selected for a group
x3
x4

I need to write a query that randomly creates a group of up to 30 employees (half male/ half female), with 15 being from tblSuffolk and 15 being from tblNassau.

In my mind's eye, I visualize a query running this. Once I'm satisified with the results of the query, I can enter a value in empSelected, so the employee cannot be selected for the next run and I could have a unique group number entered in the empGroupNumber field.

Ideally, I'd like to do this until all 900 employees have been split into groups.

Am I reaching for the stars here?

Thanks!
Art
Jan 7 '09 #1
6 1955
tdw
206 100+
Wow. I am curious as to the answer to this as well. Can Access do random number generation?

You could always create code that assigns a number to each button on the keyboard, runs some sort of calculation on a text field entry, and have a cat walk across your keyboard.
Jan 8 '09 #2
NeoPa
32,556 Expert Mod 16PB
I can see no way this could work in SQL, but VBA could do it at a stretch I think (using recordset processing).

Add an extra field to the tables and process through all the records assigning successive integer values to each record of a certain gender. That would mean maintaining two counts simultaneously for each table. The field should be indexed (may have to allow Nulls for the initial state) along with the gender field (a compound key comprising both fields).

When that's ready, your code should generate random numbers between the range you know to be available for each set of data, then grab the records in turn (flag them as selected). Finding a record that hasn't already been selected can be achieved using a "trial and error" approach if none better presents itself.

I hope this helps.

PS I love that you call your field empGender. I truly have nothing against sex, but I hate to see the term misused so often ;D
Jan 8 '09 #3
Thanks folks!!! The more I looked at what I was trying to do (and as I lay in bed staring at the ceiling)...I realized I'd have to go the "RST" route.....

I love this website...always great people to help out!

NeoPa - I have the same peeve..... :o)
Jan 9 '09 #4
NeoPa
32,556 Expert Mod 16PB
Good for you Art.

Remember. We're here if you get stuck :)
Jan 9 '09 #5
ADezii
8,834 Expert 8TB
@artemetis
The following Algorithm will Print the Random UIDs of either 7 or 8 Males (15 total Males and Females) from tblSuffolk. To generate Random Females, and to parallel the functionality in the other Table, is easy from this point on. I basically threw this together and I must admit it is a tad too much, before NeoPa states so! (LOL), but it is nonetheless a viable solution. There is definately room for improvement, and it does not check against the possibility of generating the same Record Position more than once. It did work on test data, to my surprise (LOL)!
Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim intNumOfMales As Integer
  3. Dim intNumOfFemales As Integer
  4. Dim intNumOfRecs As Integer
  5. Dim intCounter As Integer
  6. Dim intRandomPosition As Integer
  7. Dim strSQL As String
  8. Dim MyDB As DAO.Database
  9. Dim rstEmployees As DAO.Recordset
  10. Randomize
  11. 'Need 7 or 8 Males and 7 or 8 Females
  12. If Rnd() < 0.5 Then
  13.   intNumOfMales = 7
  14. Else
  15.   intNumOfMales = 8
  16. End If
  17. intNumOfFemales = 15 - intNumOfMales
  18. 'Do the Males first, forget the Females for now
  19. strSQL = "Select * From tblSuffolk Where [empGender] = 'M';"
  20. Set MyDB = CurrentDb
  21. Set rstEmployees = MyDB.OpenRecordset(strSQL, dbOpenDynaset)
  22. With rstEmployees
  23.   .MoveLast
  24.   .MoveFirst
  25.     intNumOfRecs = .RecordCount       'Number of Males in tblSuffolk
  26.   'Pick intNumOfMales Random Record Positions, move to that Record,
  27.   'then Print the result
  28.   For intCounter = 1 To intNumOfMales
  29.     intRandomPosition = Int(Rnd * intNumOfRecs) + 1
  30.       If intRandomPosition = 1 Then
  31.         Debug.Print rstEmployees![empUID]
  32.       Else
  33.         .Move intRandomPosition - 1
  34.         Debug.Print rstEmployees![empUID]
  35.       End If
  36.       rstEmployees.MoveFirst
  37.   Next
  38. End With
  39.  
  40. rstEmployees.Close
  41. Set rstEmployees = Nothing
  42.  
Jan 10 '09 #6
NeoPa
32,556 Expert Mod 16PB
@ADezii
I wouldn't dream of it ADezii ;D
Jan 11 '09 #7

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

Similar topics

1
by: snOOp | last post by:
I am trying to combine the data from two similar tables into one query, but I need for all of the records from both tables to show up and I want the ones that have matching 'emplid' to be combined...
5
by: Jamie Pittman via AccessMonster.com | last post by:
I have two tables with 5000 entries on them. One is based for regular time with several variables example (employee name, date,time in and out, code, customer, building) I have another table that...
1
by: mojo | last post by:
We have set up an Access database with tables x,y, & z where we would like to have multiple people entering data into a table x. Each person has an identical copy of the database on their PC's. ...
2
by: Reidar Jorgensen | last post by:
I have several Access databases, identical in structure, but different data. Is there an easy way to combine them all into one database? There are six tables, I just want the data from all...
1
by: Red | last post by:
So I inherited this db that has multiple 'orders' tables. Each order table is name for the year of the orders... i.e. 2005 is 'Orders-2005', 2006 is "Orders-2006" So, I am not allowed to...
1
by: Roy | last post by:
No errors here, I'm just looking for ways to make my code more efficient. As the subject line suggests, I have a LOT of repeaters on a certain page. They are visible or hidden depending on user...
2
by: nugz | last post by:
I want to combine 3 tables with like data then append it with a filter. Tables: NewStarts, Complaints, Memos Combine: Date, Address, Route, Pub, etc.... Then sort: previous 8 days, pub/freq...
1
by: bluereign | last post by:
Thank you for your assistance. I am a novice looking to JOIN and append or combine records from 2 current Tables into 2 new Tables named below. I have been able to JOIN Tables with the script...
3
by: stateemk | last post by:
Hi, I'm probably making this much harder than it needs to be, but I just can't think of how to do this. Each year, a database is created with all employees on it. I need to pull all the employees...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.