We help IT Professionals succeed at work.

Access Replace Function

Medium Priority
408 Views
Last Modified: 2012-05-11
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
Comment
Watch Question

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

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
Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Harry BattDirector of Development

Author

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.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.