inserting a value with an '

This code errors because rs!StagingField has a value with a ' in it like this "john smith's house".  

  strUpdate = "UPDATE Master" & _
   " SET Master." & strFieldName & "='" & rs!StagingField & "'

   CurrentDb.Execute strUpdate, dbFailOnError
vbnetcoderAsked:
Who is Participating?
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.

Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
My method of handling it:

 strUpdate = "UPDATE Master" & _
   " SET Master." & strFieldName & "=" & chr$(34) & rs!StagingField & chr$(34)

as I don't often see a quote in text.  

But there are a variety of them.  Some call a custom function replacing all single and double (apostrophe and quote) with a double character so it works, use a defined constant, or simply strip them out.

Jim.
0

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
vbnetcoderAuthor Commented:
ty
0
Dale FyeCommented:
I use one of those functions Jim mentioned:

Public Function fnWrap(WrapWhat as Variant, Optional WrapWith as string = """") as String

    if isNull(WrapWhat) Then
        fnWrap = "NULL"   'this returns a text string NULL rather than the value NULL
    else
        fnWrap = WrapWith & Replace(WrapWhat, WrapWith, WrapWith & WrapWith) & WrapWith
    end if
End Function

Open in new window

Then I call this as:

 strUpdate = "UPDATE Master SET Master.[" & strFieldName & "] =" & fnWrap(rs!StagingField)

By default, it uses quotes, but you can use ' as the WrapWith character to pass values to SQL Server, or even # to wrap a date value.

BTW,  I added brackets around your strFieldName because occassionally, we have to deal with field names with spaces or which are reserved words, and it is safer to wrap the field name, just in case.

Dale
0
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
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.