This topic is locked
[SOLVED]

 Prevent Duplicate Entries - Multiple Fields

7/18/2012 12:30:23 PM
ASPRunnerPro General questions
M
McGowan author

My project has a table that has multiple fields for recording information about a client. Four of those fields must be unique for each client and, therefore, I need to prevent duplicate records from being created. I've successfully created a Before Add event that checks two of the Fields (LegalName and DBA) but the other two fields are more complicated and I don't know how to deal with them. Fields 3 and 4 are based on Industry Codes unique to each client; however, some clients don't have such codes assigned to them. Since this is a mandatory field, we've set a policy that a hyphen "-" be entered for those that don't have such codes. In hindsight, this may not have been such a good idea but it is what it is.
As a result, I do not know how to create an event that checks these values for existing records while ignoring values of “-“.
For clarity, all four fields must be checked for uniqueness. I cannot have any of the fields containing duplicate information, regardless if some matches and some does not. It is not an “And” situation but an “Or” one.
Here are my fields that cannot have duplicates (all are indexes):
Field1 = LegalName

Field2 = DBA

Field3 = IndustryCode

Field4 = IndustryCode2
Here is my code that checks Field1 and Field2:



'********** Check if specific record exists ************

dim rsExists

set rsExists = dal.Clients.Query("LegalName='" & values("LegalName") & "' or DBA='" & values("DBA") & "'","")

if not rsExists.eof then

message = "<font color=red>A Client with this Legal Name or DBA already exists!

Please check the Client list before adding a new record.</font>"

BeforeAdd = false
else
BeforeAdd = True
end if

rsExists.Close : set rsExists = Nothing


Question 1: How do I include Fields 3 and 4 into "rsExists" while ignoring entries of "-".
Question 2: How do I customize the message so that I can tell the user which of the four fields contained duplicate information?
Question 3: How do I apply this on the Edit page to prevent existing records from becoming duplicates?
Thank you.

Sergey Kornilov admin 7/19/2012

Some manual coding will be required here. Here is a bit of advice.

  1. If dash character may appear at any position in legal name or in DBA you need to add two additional fields to the database to store normalized (dashes and other characters removed) version of both legal name and DBA. You will need to add code to BeforeEdit/BeforeAdd events in order to populate those additional fields.
    Once you done that you can run a query to find duplicates:
    GoodLegalName=Replace(values("LegalName"),"-","")

    GoodDBA=Replace(values("DBA"),"-","")
    SQL = "select * from Clients where GoodLegalName='" & GoodLegalName & "' or GoodDBA = '" & GoodDBA & "'"
  2. If matches found compare entered values with database values. Perform this for each field separately to display a customized message:

if GoodLegalName=rsExists("GoodLegalName") then

message = "<font color=red>A Client with this Legal Name already exists!

Please check the Client list before adding a new record.</font>"

end if


3. You need to implement both BeforeAdd and BeforeEdit events in order to check for duplicates after editing as well.
Contact support team directly if you are looking for a paid help with this.

M
McGowan author 7/19/2012

Sergey,
Thank you for your reply. I believe you may have misunderstood me slightly. My problem does not exist with the fields LegalName or DBA; I have those two in my Event and they are working as desired. The problem is that I want to add the fields IndustryCode1 and IndustryCode2 to the Event and check those for duplicates as well. I want the event to ignore entries of "-" when evaluating IndustryCode1 and IndustryCode2.
I will attempt to apply the same logic to those fields as you provided for LegalName and DBA. I don't see why it shouldn't work. If I cannot get it to work, I will let you know.
Thank you for the help. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=67088&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />