Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading String with single quote ' from excel 1

Status
Not open for further replies.

sgupta76

Programmer
Nov 26, 2003
34
0
0
US
I am trying to read a string from an excel field that has a single quote within it. For instance : "St.John's Hospital".

I pass the value into variable say 'sString'
Then I run the statement in VBA
Docmd.RunSql "Insert into Table1(Name) values('" & sString & "')

Since I am passing the string with a single quote the statement gives me a syntax error because of the single quotes. How can I read the string from excel to avoid getting the syntax error?

Thanks for your help

 
Try either:

[tt]...values(""" & sString & """)"[/tt]

or

[tt]...values('" & replace(sString,"'","''" & "')"[/tt]

Roy-Vidar
 
Oups - missed a closing parens in the last one:

[tt]...values('" & replace(sString,"'","''") & "')"[/tt]

Roy-Vidar
 
Thanks much. the first solution worked!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top