I have some lines of code in the after update event of a text box that insert a new record in another table. One of that new record's fields is the foreign key of the current selected record in the form. The problem is that it must insert only one record and a second unnecessary record is automatically inserted as well with the same values (except for the primary key which is an autonumber) as the first record. I tried to make the foreign key unique but when I am in the main form and move the focus to an other record to save the changes it doesn't let me commit the change since it would violate the uniqueness constraint, which means the second unnecessary record is trying to be inserted. What is the reason this second record is automatically inserted?
Maybe there is a problem with my code:
Private Sub txtReturnDate_AfterUpdate()
'If the Return date is after the dueback date a late fee is calculated and related to the current invoice
If txtReturnDate.Value > DueBack.Value Then
Dim daysLate As Integer
Dim sSQL As String
Dim LateFee As Integer
Dim temp_fk_val As Integer
daysLate = ReturnDate.Value - DueBack.Value
LateFee = daysLate * PricePerUnitRental.Value 'Calculates the late fee amount
If LateFee >= PurchasePrice.Value Then LateFee = PurchasePrice.Value 'The maximum late fee amount is the purchase price of the video
sSQL = "INSERT INTO tblLateFees (LateFeeAmount, InvoiceDetailID) VALUES (" & LateFee & ", " & InvoiceDetailID & " );"
DoCmd.RunSQL sSQL
txtLateFeeAmount.Value = LateFee
End If
End Sub
Maybe there is a problem with my code:
Private Sub txtReturnDate_AfterUpdate()
'If the Return date is after the dueback date a late fee is calculated and related to the current invoice
If txtReturnDate.Value > DueBack.Value Then
Dim daysLate As Integer
Dim sSQL As String
Dim LateFee As Integer
Dim temp_fk_val As Integer
daysLate = ReturnDate.Value - DueBack.Value
LateFee = daysLate * PricePerUnitRental.Value 'Calculates the late fee amount
If LateFee >= PurchasePrice.Value Then LateFee = PurchasePrice.Value 'The maximum late fee amount is the purchase price of the video
sSQL = "INSERT INTO tblLateFees (LateFeeAmount, InvoiceDetailID) VALUES (" & LateFee & ", " & InvoiceDetailID & " );"
DoCmd.RunSQL sSQL
txtLateFeeAmount.Value = LateFee
End If
End Sub