Okay here is the solution:
1) First create a yes/no field in TestResults (table) and name it SaveToVTest, set its default value to true (-1) so it will be true for each new entry.
2) Add this line of code before you enter list items to TestResults table:
Code:
'clear all the checks from TestResults (SaveToVTest field)
DoCmd.RunSQL ("UPDATE TestResults SET TestResults.SaveToVTest = 0;")
This will clear all checks before Inserting new entry to TestResults table.
3) Add this line of code to Append/Insert the newly added record to VehicleTestInformation table.
Code:
strSQL = "INSERT INTO VehicleTestInformation ( TestResultFK, VehicleFK )" & _
" SELECT TestResults.TestResultID, TestResults.TestFK " & _
" FROM TestResults " & _
" WHERE (((TestResults.SaveToVTest)=True));"
DoCmd.RunSQL (strSQL)
This code will append all the records WHERE (((TestResults.SaveToVTest)=True))
your full code is here:
Code:
Private Sub butSave_Click()
Dim strSQL As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim ctl As Control
Dim varItem As Variant
On Error GoTo ErrorHandler
Set db = CurrentDb()
Set rs = db.OpenRecordset("TestResults", dbOpenDynaset, dbAppendOnly)
Dim CBx As ComboBox, DL As String
Set CBx = Me![cboVehicle]
DL = vbNewLine & vbNewLine
If CBx.ListIndex = -1 Then 'No selection made!!
MsgBox "'Vehicle' is a required field!" & DL & _
"You'll have to go back and make a selection ...", _
vbInformation + vbOKOnly, _
"Missing Data Error! . . ."
CBx.SetFocus
Cancel = True
Exit Sub
End If
Set CBx = Nothing
'make sure a selection has been made
If Me.listComponents.ItemsSelected.Count = 0 Then
MsgBox "Must select at least 1 component variant you wish to Test"
Exit Sub
End If
'clear all the checks from TestResults (SaveToVTest field)
DoCmd.RunSQL ("UPDATE TestResults SET TestResults.SaveToVTest = 0;")
'add selected value(s) to table test results
Set ctl = Me.listComponents
For Each varItem In ctl.ItemsSelected
rs.AddNew
rs!ComponentFK = ctl.ItemData(varItem)
rs!TestFK = Me.cboTest
rs!Result = "Open"
rs.Update
Next varItem
'now append the TestResults data to VehicleTestInformation table
strSQL = "INSERT INTO VehicleTestInformation ( TestResultFK, VehicleFK )" & _
" SELECT TestResults.TestResultID, TestResults.TestFK " & _
" FROM TestResults " & _
" WHERE (((TestResults.SaveToVTest)=True));"
DoCmd.RunSQL (strSQL)
ExitHandler:
Set rs = Nothing
Set db = Nothing
Exit Sub
ErrorHandler:
Select Case Err
Case Else
MsgBox Err.Description
DoCmd.Hourglass False
Resume ExitHandler
End Select
End Sub
Hope this would solve the problem.