How do I use a record only once when making a table from a query?

G

Guest

I am making a database that will allow me to create records for students and
participants in our College Weekend event. Students will host College
Weekenders in their rooms. I want to match College Weekenders to Students
according to Major. I have two tables: Students and CWtable both of which
have a "major" field. My query is a "create table" query. I joined the two
"major" fields together and selected that only to include rows that are equal.

The query runs fine, but assigns each College Weekender to each Student with
the same major. I only want to assign one College Weekender to one Student.
 
D

Duane Hookom

If you don't have your students and weekenders numbered (ordered), you won't
get this to work.
 
G

Guest

I auto numbered each record then ran the query again with the same results.
What other steps do I need to take?
 
C

Chris2

altonomus said:
I am making a database that will allow me to create records for students and
participants in our College Weekend event. Students will host College
Weekenders in their rooms. I want to match College Weekenders to Students
according to Major. I have two tables: Students and CWtable both of which
have a "major" field. My query is a "create table" query. I joined the two
"major" fields together and selected that only to include rows that are equal.

The query runs fine, but assigns each College Weekender to each Student with
the same major. I only want to assign one College Weekender to one Student.

altonomus,

You mention you have a CREATE TABLE query, but you say you joined two
major fields in it (sounds like a SELECT), and that you "selected that
only to include" (which also sounds like a SELECT).

But, aside from that, I can't think of how except by VBA:

Air Code to follow:

Public Sub sAssignCW()

Dim db As DAO.Database
Dim rsStudents As DAO.Recordset
Dim rsCWtable As DAO.Recordset
Dim rsMajors As DAO.Recordset
Dim SQL_Students As String
Dim SQL_CWtable As String
Dim SQL_Majors As String
Dim CurrentMajor As String

SQL_Majors = "SELECT DISTINCT M1.Majors " & _
" FROM Students AS M1 "

Set db = CurrentDb()

set rsMajors = db.OpenRecordset(SQL_Majors, dbOpenForwardOnly)

'Insert EOF file checking

do until rsMajors.eof

CurrentMajor = rsMajors.Fields("Majors")

SQL_Students = "SELECT * " & _
" FROM Students AS S1 "
" WHERE S1.Majors = " & CurrentMajor & ";"

SQL_CWtable = "SELECT * " & _
" FROM CWtable AS C1 "
" WHERE C1.Majors = " & CurrentMajor & ";"

Set rsStudents = db.OpenRecordset(SQL, dbOpenDynaset)
Set rsCWtable = db.OpenRecordset(SQL_CWtable, dbOpenForwardOnly)

'Insert EOF file checking

With rsStudents
.MoveFirst
Do Until .EOF
.Edit
.Fields("CollegeWeekender") = rsCWtable.Fields("CWtableID")
.Update
.MoveNext
rsCWtable.MoveNext
Loop
End With

rsStudents.Close
rsCWtable.Close
rsStudents = Nothing
rsCWtable = Nothing

rsMajors.MoveNext

Loop

rsMajors.Close
rsMajors = Nothing
db.Close
db = Nothing

' Along with the giantic assumption that there cannot
'be more College Weekenders of a particular Major
'than there are Students of a particular Major.
' Extra code will be required to handle that
'situation (which doesn't look pretty at this point).

End Sub


Yes, it is untested. My apologies, but I don't have the table
structures handy. But the basic idea *should* work.

1: Summon up unique list of majors.
2: Assign CWtable's PK to a new column in Students, "WHERE" Majors
match.


Sincerely,

Chris O.
 
G

Guest

Chris,

I ran into a syntax error on these lines. I don't have a clue about VB so
I'm sure that it is an easy fix. Thanks for your great help thus far.
 
C

Chris2

altonomus said:
Chris,

I ran into a syntax error on these lines. I don't have a clue about
VB

altonomus,

That makes now an excellent time to learn! :)

so
I'm sure that it is an easy fix. Thanks for your great help thus far.

Well, I did say it was untested.

The ends of the lines need an Ampersand and an Underscore, thus:

SQL_Students = "SELECT * " & _
" FROM Students AS S1 " & _
" WHERE S1.Majors = " & CurrentMajor & ";"

SQL_CWtable = "SELECT * " & _
" FROM CWtable AS C1 " & _
" WHERE C1.Majors = " & CurrentMajor & ";"

Ampersand, &, is the concatentation operator, it combines strings
together.

Underscore, _, is the line-continuation operator, it says the next
line is a part of the same statement as the current line. It's used
to aid the readability of code (and to keep lines from going off the
side of the screen.



Again, without tables to run it on myself, I'm not sure it will work.

Also, remember, I "invented" a column for Students on your behalf
called CollegeWeekenders. If you already have a similar column in
Students, you'll need to rename the field in the VBA code.

If you don't have such a column, my code won't work.

Plus there are the other warnings I tossed in earlier.


Sincerely,

Chris O.
 
D

Duane Hookom

You could try to join the autonumber fields. I do however agree with Chris2
that the more robust solution involves code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top