Question : Access DCount Comparison

I want to ensure that no duplicate records are being added to a lookup table.  I have code, which traps duplicates, except for organizations that have an apostrophe in their name.  Then I get a syntax error.  I am new to vba and am fairly certain the " ' " is the issue, but I'm not at all certain how to fix it.  What would be the correct syntax to ensure all duplicates are trapped?
Code Snippet:
1:
2:
3:
If DCount("OrganizationName", "tblOrganizationsMaster", "[OrganizationName]='" & Me.CompanyName & "'") > 0 And DCount("[Org Address]", "tblOrganizationsMaster", "[Org Address]='" & Me.Address & "'") > 0 Then
    MsgBox "An organization with this address already exists.", vbExclamation, strAppName
    End If

Answer : Access DCount Comparison

You must use the Replace function to do this:

Replace(Me.CompanyName, "'", "''")

Note, however, that your DCount function may not catch everything that you wish. In your case, assuming that you wish to catch a duplicate where the CompanyName AND the Address are the same IN THE SAME RECORD, you'd need to do this:

If DCount("OrganizationName", "tblOrganizationsMaster", "[OrganizationName]='" & Replace(Me.CompanyName,"'", "''") & "' AND [Org Address]='" & Me.Address & "'") > 0 Then
    MsgBox "An organization with this address already exists.", vbExclamation, strAppName
    End If
 
Random Solutions  
 
programming4us programming4us