Results 1 to 9 of 9
  1. #1
    rubintheyid is offline Novice
    Windows 98/ME Access 2003
    Join Date
    Mar 2010
    Posts
    4

    Delete record if duplicate

    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

  2. #2
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    Your Data entry form, is it Bound or an Unbound Form. Please make that clear.

  3. #3
    rubintheyid is offline Novice
    Windows 98/ME Access 2003
    Join Date
    Mar 2010
    Posts
    4
    Hi Maximus,

    the form is unbound.

    Cheers

  4. #4
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    you say unbound then I will assume that you are using sql to append data to your table/tables. now if it is a bound form once you type the data onto a Field on the form a new entry is created so you have to delete to cancel it. but if it is unbound then you can just stop the execution on the sql and data is not entered into the table. Now bear with me, do you have another button on the form to enter data. if yes please post the code.

  5. #5
    rubintheyid is offline Novice
    Windows 98/ME Access 2003
    Join Date
    Mar 2010
    Posts
    4
    Hi Maximus,

    The field is added once we tab down to the next field to populate so we do not have a button to confirm the add. Hope this makes sense I am still very new to this!!

  6. #6
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    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.

    DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
    DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70


    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

    add the lines marked in red.











  7. #7
    rubintheyid is offline Novice
    Windows 98/ME Access 2003
    Join Date
    Mar 2010
    Posts
    4
    That works.. Many thanks for your help.

  8. #8
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    mark the thread solved please this will help somebody with the same problem find quick answers. Go to thread tools and selected solved.

  9. #9
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    mark this thread solved please.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. If duplicate record, go to original record
    By BigBear in forum Forms
    Replies: 2
    Last Post: 10-06-2010, 02:58 AM
  2. If duplicate record, go to original record
    By islandboy in forum Access
    Replies: 51
    Last Post: 06-02-2010, 04:17 PM
  3. Delete duplicate records
    By Zukster in forum Queries
    Replies: 1
    Last Post: 08-26-2009, 03:14 AM
  4. duplicate record based on calculation
    By Coolpapabell in forum Queries
    Replies: 4
    Last Post: 08-06-2009, 07:53 AM
  5. Duplicate record failure
    By bugchaser in forum Access
    Replies: 5
    Last Post: 05-21-2009, 08:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums