The ID (AFD_ID as JR_ID) should be obtained from Personnel, not JR_TrClassesT (see Post #20).Perhaps the ID is incorrect?
Code:"AND AFD_ID NOT IN (SELECT JR_ID FROM JR_TrClassesT)
Should that JR_ID be AFD_ID ?, or vice versa?
I am attempting to grab the 12 Juniors from the Personnel table (no duplicates here, 1 record for each member including the 12 Junior members) and create new records for a specific training class in JR_TrClassesT (many records per Junior member; they train one night a week all year round).
strSQL = "INSERT INTO TrClassesT(JR_ID,TrnDate,TrnType) " & _
"SELECT AFD_ID, #" & Format(Me.TrnDate,"yyyy-mm-dd") & "#,""" & Me.TrnType & """ " & _
"FROM Personnel WHERE Rank = ""Junior FireFighter"" AND Status = ""Active"""
Private Sub cboTrnType_AfterUpdate()
Const MESSAGE_TEXT = "A training date must be selected."
Dim strSQL As String
If Not IsNull(Me.TrnDate) Then
strSQL = "INSERT INTO JR_TrClassesT(JR_ID,TrnDate,TrnType) " & _
"SELECT AFD_ID, #" & Format(Me.TrnDate, "yyyy-mm-dd") & "#,""" & Me.TrnType & """ " & _
"FROM Personnel WHERE Rank = 'Junior FireFighter' AND Status = 'Active' ORDER BY [Last Name], [First Name];"
CurrentDb.Execute strSQL
Me.[JR_TrClassesT subform].Form.Requery
Else
MsgBox MESSAGE_TEXT, vbExclamation, "Invalid Operation"
End If
End Sub
Better: CurrentDb.Execute strSQL, dbFailOnError
Ken, in all my decades of Executing SQL statements, either I wasn't aware of that "feature" or forgot it decades ago.Tom, I deliberately omitted the dbFailOnError constant in my earlier reply to the OP. This allows additional trainees to be added to a training session by executing the procedure again for that session. The errors which are raised on those rows which attempt to insert a trainee who has already been inserted are thus ignored, and only any new trainees are added. If the dbFailOnError option is included, then the SQL statement should be extended by adding a WHERE clause to exclude those rows where the referencing table already includes a trainee/session which matches the trainee in the referenced table/the current session.
It's much the same as when you run the query in the user interface, and it warns you that a certain number of rows won't be inserted for a stated reason, in this case a key violation. In the example I posted for the OP I tried to keep things simple, but I have to confess that it's not great code, and contradicts 'Einstein's razor' that things should be done as simply as possible, but no more so. If another error were to occur other than the key violation on a row which was otherwise valid, that row would be ignored also. It would be better to include the dbFailOnError option, and restrict the rows inserted to only new trainees as I described in my last post.Ken, in all my decades of Executing SQL statements, either I wasn't aware of that "feature" or forgot it decades ago.
'It can scarcely be denied that the supreme goal of all theory is to make the irreducible basic elements as simple and as few as possible without having to surrender the adequate representation of a single datum of experience.'
Albert Einstein "On the Method of Theoretical Physics" The Herbert Spencer Lecture, delivered at Oxford (10 June 1933)
Bingo!! Thank you ALL for your input; many lessons learned (as always) from the gurus, some of which I have leaned on from UA (you know who you are). Here's the final code:
Code:Private Sub cboTrnType_AfterUpdate() Const MESSAGE_TEXT = "A training date must be selected." Dim strSQL As String If Not IsNull(Me.TrnDate) Then strSQL = "INSERT INTO JR_TrClassesT(JR_ID,TrnDate,TrnType) " & _ "SELECT AFD_ID, #" & Format(Me.TrnDate, "yyyy-mm-dd") & "#,""" & Me.TrnType & """ " & _ "FROM Personnel WHERE Rank = 'Junior FireFighter' AND Status = 'Active' ORDER BY [Last Name], [First Name];" CurrentDb.Execute strSQL Me.[JR_TrClassesT subform].Form.Requery Else MsgBox MESSAGE_TEXT, vbExclamation, "Invalid Operation" End If End Sub
Merry Christmas!Happy Holidays!
Stay Safe!
![]()
[name-of-sub-form-control].Form.Requery