Hi, I am trying to build a form to enter invoice details. I want to ensure that no duplicates are entered so have written the below. This should identify if its a duplicate and if so a message box appears and gives the option to delete if No is pressed ammendments can be made if YES is selected I need the reecord to be deleted. Its the YES i can not get to work. Any help appreciated. Here is the script:
Option Compare Database
Private Sub InvoiceNumber_BeforeUpdate(Cancel As Integer)
On Error GoTo err_InvoiceNumber_BeforeUpdate
Dim strCheckSQL As String
Dim strCP As String
Dim strIN As String
Dim rstCheck As Recordset
Dim intTest As Integer
strCP = Me.cboContentProvider
strIN = Me.InvoiceNumber
'This will assign SQL code to variable strCheckSQL
strCheckSQL = "SELECT ContentProvider, InvoiceNumber FROM PayableRegister WHERE (((ContentProvider)=""" & strCP & """) AND ((InvoiceNumber)=""" & strIN & """));"
Set rstCheck = CurrentDb.OpenRecordset(strCheckSQL)
If rstCheck.RecordCount <> 0 Then
Response = MsgBox("Invoice number already exists for CP. Do you want to delete the record?", vbYesNo, "Duplication Error")
If Response = vbYes Then ' User chose Yes.
Else ' User chose No.
Cancel = True
Me.InvoiceNumber.Undo
End If
End If
rstCheck.Close
exit_InvoiceNumber_BeforeUpdate:
Exit Sub
err_InvoiceNumber_BeforeUpdate:
MsgBox Err.Description
Resume exit_InvoiceNumber_BeforeUpdate
End Sub