Solved

Prevent duplicate records

Posted on 2014-02-25
2
209 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
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…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now