Undesired and misterious double insertion

PepeGallo

Registered User.
Local time
Today, 07:56
Joined
May 2, 2008
Messages
14
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
 
Perhaps you are using a bound form to insert data into the table. In the afterupdate of a textfield, you are inserting a record using an sql statement.

Loose the sql statement or use unbound form. My choice would be to use unbound forms.

HTH:D
 

Users who are viewing this thread

Back
Top Bottom