Hello,
I have a database where teachers can create new tests with the click of a button so I need a dynamic way to add new results for the students.
To do this I made the following button:
The button works perfectly but it has a major flaw, I don't know how to check if a certain combination already exists (the primary key is generated). So I could make it a primary key consisting of two foreign keys and disable warnings or, what I preffer, is a way to handle this in the code. My coding isn't very good though and I don't really know what the best way would be to handle this.
So my question is:
How do I check if my INSERT INTO doesn't add a combo of [studentindex] and [toetscode] that already exists.
I have a database where teachers can create new tests with the click of a button so I need a dynamic way to add new results for the students.
To do this I made the following button:
Code:
Private Sub btnToetsen_Click()
Dim sSQL As String
Dim toetsQuery As String
Dim studentindex As Integer
Dim opleidingid As Integer
studentindex = Me.tbStudentindex
opleidingid = Me.tbOpleidingid
toetsQuery = "SELECT Student.studentindex, Toets.toetscode FROM (Koppelvak INNER JOIN Toets ON Koppelvak.vakcode = Toets.vakcode) INNER JOIN Student ON Koppelvak.opleidingid = Student.opleidingid WHERE (((Student.studentindex)=" & studentindex & ") AND ((Student.opleidingid)=" & opleidingid & "))"
sSQL = "INSERT INTO Resultaat (studentindex, toetscode)" & toetsQuery
MsgBox (sSQL)
DoCmd.RunSQL (sSQL)
DoCmd.Requery
End Sub
The button works perfectly but it has a major flaw, I don't know how to check if a certain combination already exists (the primary key is generated). So I could make it a primary key consisting of two foreign keys and disable warnings or, what I preffer, is a way to handle this in the code. My coding isn't very good though and I don't really know what the best way would be to handle this.
So my question is:
How do I check if my INSERT INTO doesn't add a combo of [studentindex] and [toetscode] that already exists.