Question

I have two tables (tblCurrentStudents and tblNewStudents) in an Access file. tblCurrentStudents was current at of the end of the last school year and contains a list of last year's students and their information. tblNewStudents contains a complete list of this year's student information. There are some students common to both tables, meaning they were here last year and are here this year. However, in tblCurrentStudents there are some students who were NOT here last year, but enrolled at the beginning of this year.

What I need to do now is remove the students from tblCurrentStudents who are no longer enrolled, meaning they left at the end of last year and will not be in tblNewStudents.

I know I can join two tables together using a Union command (I found the necessary code to do this). I don't know how to do the opposite, meaning find those records not common to both tables and remove just those records from tblCurrentStudents.

Any suggestions? Is there such as thing as a Not Union command?

Was it helpful?

Solution

Assuming I'm understanding your question correctly, you want to remove any student from tblCurrentStudents who are not in tblNewStudents?

If so, I think you're looking to use IN():

DELETE FROM tblCurrentStudents
WHERE StudentId NOT IN (
    SELECT StudentID FROM tblNewStudents)

OTHER TIPS

I created a practice set of tables that simulate what I want. This is the UNION code I found on the Internet and used:

Private Sub Command0_Click()

Dim sql As String, sTable As String, oTD As TableDef sql = "SELECT * FROM MergeTable UNION SELECT * FROM MainTable"

If MsgBox("Store unique records in a Table?", vbYesNo) = vbYes Then sTable = InputBox("Which table name?", , "tblMerged")

For Each oTD In CurrentDb.TableDefs
    If oTD.Name = sTable Then sTable = sTable & "1"
Next oTD

sql = "SELECT * INTO " & sTable & " FROM (" & sql & ")"
DoCmd.RunSQL sql

End If

End Sub

Assuming I'm understanding your question correctly, you want to remove any student from tblCurrentStudents who are not in tblNewStudents?

Yep, that's exactly what I need. I'm going to try the IN command. I'll let you know how it goes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top