Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

Access Replace Function

On a form, I have created the following code that traps any duplicates before being entered.  The expression is supposed to check and see if there is a match between the form's "ContactID" and the "fk_ContactID" value in the table "tblSolicitors".  Upon running the code I get a data type mismatch error.  This has worked before using text fields, but I have never tried it with number fields.  What am I doing wrong?

 If DCount("fk_ContactID", "tblSolicitors", "[fk_ContactID]='" & Replace(Me.ContactID, "'", "''") & "'") > 0 Then
    MsgBox "This solicitor already exists.", vbExclamation, strAppName
    End If
0
Harry Batt
Asked:
Harry Batt
  • 3
1 Solution
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Assuming ContactID is Numeric, try this:

    If DCount("fk_ContactID", "tblSolicitors", "[fk_ContactID]=" & Replace(Me.ContactID, "'", "''")) > 0 Then
         MsgBox "This solicitor already exists.", vbExclamation, strAppName
    End If

mx
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
If ContactID is Text:

    If DCount("fk_ContactID", "tblSolicitors", "[fk_ContactID]=" & Chr(34) & Replace(Me.ContactID, "'", "''") & Chr(34)) > 0 Then
         MsgBox "This solicitor already exists.", vbExclamation, strAppName
    End If

mx
0
 
Harry BattDirector of DevelopmentAuthor Commented:
Thanks Database MX-and especially for showing me the comparison between how to handle a numeric and text field in this situation.  It is a numeric field by the way.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
You are welcome.
When text, you have to surround the criteria with Double quotes, which is Chr(34) - used for clarity in posting.

mx
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now