Microsoft Access Double Click Event chokes on the apostrophe (') and quote (") symbols.

I built an Access database that stores our "Office Supplies" with several reports that when generated I can "Double-Click" a line item in that report and open it's corresponding record in a form.  I did this using a macro in the Double Click 'Event' Property.

It was giving a syntax error when it encountered any record had an apostrophe within the ItemName field.  To get around this I wrote the macro:
="[ItemName]=" & Chr(34) & [ItemName] & Chr(34)

This worked great! But I recently found out that I got the same Macro Syntax error when it encountered a quote (") symbol in the ItemName field of any record.

My question is:  How can I properly incorporate both Chr(34) AND Chr(39) strings in this Event macro?

I have tried every way I know of to And/Or it but have been failing miserably.  What is the right way to do this?
cruderhaysAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PatHartmanCommented:
If your string might contain either single or double quotes, you have to "escape" them.  Do that with a Replace().  You'll need two of them.  One to change the  one single quote to two single quotes and the other to change the one double quote to two double quotes.  Your string will end up like:

Patrick O''Brian

That's two single quotes, not one double but it's hard to see.
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
if the data HAS what you are using to delimit in it, you can replace them with 2
="[ItemName]=" & Chr(34) & Replace( [ItemName] ,"""", """""")  & Chr(34)

Open in new window


"" inside a string delimited with " gives you just 1 " when things are done ~

have an awesome day,
crystal

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale FyeOwner, Developing Solutions LLCCommented:
I use a function:

Public Function fnQuotes(QuoteWhat as Variant, Optional QuoteWith as String = """") as String

    If IsNull(QuoteWhat) Then
        fnQuotes = "NULL"
    Else
        fnQuotes = QuoteWith & Replace(QuoteWhat, QuoteWith, QuoteWith & QuoteWith) & QuoteWith
    Endif

End Function

Open in new window

You can use this with text strings, or with date literals as well

="[ItemName]=" & fnQuotes([ItemName])

or if you want to use single quotes

="[ItemName]=" & fnQuotes([ItemName] , chr$(39))

or, if you want to encapsulate dates in #:

= "[DateField] >= " & fnQuotes(dtStartDate, "#")

HTH
Dale
cruderhaysAuthor Commented:
That worked perfectly!  Thank you so much and thanks to all who made suggestions.  I will use this code and others in upcoming projects for sure.
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
you're welcome ~ happy to help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.