How to check for duplicate record before inserting

Posted on 2008-11-19
Medium Priority
Last Modified: 2013-11-27
I have a table that I am inserting records into based on certain criteria. The code is as follows:

CurrentDb.Execute ("INSERT INTO [Activities] (ClaimNo, Custodian, Activity, DateStamp) VALUES ('" & Me.pendclaimnum & "','" & Me.Custodian1 & "','PCAC',#" & Int(Now()) & "# )")

I'd like to make it so, if the values of Me.pendclaimnum and PCAC already exist in the table, not to insert them. Would this be achieved by running a select query and then comparing the recordset/results? Not quite sure how to approach this, It needs to run relatively fast as well... Any help would be awesome!

Question by:c9k9h
  • 2
LVL 120

Accepted Solution

Rey Obrero (Capricorn1) earned 2000 total points
ID: 22998298
you can use a dlookup or dcount

if dcount("ClaimNo","Activities","[ClaimNo]='" & Me.pendclaimnum & "' And [Activity]='PCAC'")>0 then

msgbox "Record already exists"


CurrentDb.Execute ("INSERT INTO [Activities] (ClaimNo, Custodian, Activity, DateStamp) VALUES ('" & Me.pendclaimnum & "','" & Me.Custodian1 & "','PCAC',#" & Int(Now()) & "# )")

end if


Author Comment

ID: 22998441
I always seem to over complicate things and neglect those dlookup functions!


Author Closing Comment

ID: 31518444

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

839 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