Solved

Prevent duplicate records

Posted on 2014-02-25
2
213 Views
Last Modified: 2014-02-25
Am expert had helped me with the following code to prevent duplicate entries in a table and I suppose it works but if a form is opened to review a current record and then the form is closed they get a message indicating that the record already exists.  I have this code in the onclose event of the form.

How can I fix this problem?  We definitely don't want duplicate records but we do want to be able to review an existing record.

Here's the code:

    If DCount("*", "tblProducts", "ProdName & SizeID & FormatID & LocationID = " & Chr(34) & Me.cboProdName & Me.cboSizeID & Me.cboFormatID & Me.cboLocationID & Chr(34)) > 0 Then
        MsgBox "This product already exists.  Please correct your entry."
        Me.cmdCloseForm.SetFocus
        Cancel = True
    End If

Open in new window

0
Comment
Question by:SteveL13
  • 2
2 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39886663
Try moving the code to the Before Update event of the form... which only runs if the record has changed.
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39886678
Alternatively, since you are using a custom close button... move the code to the Click Event of cmdClose, but add a check to only run the code if the form is dirty:

If Me.Dirty = True then
    If DCount("*", "tblProducts", "ProdName & SizeID & FormatID & LocationID = " & Chr(34) & Me.cboProdName & Me.cboSizeID & Me.cboFormatID & Me.cboLocationID & Chr(34)) > 0 Then
        MsgBox "This product already exists.  Please correct your entry."
        Me.cmdCloseForm.SetFocus
        'Cancel = True  <<--- needed in the Before Update event, not in a command button click event
    End If
End if

Open in new window

0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question