Variable contains ' and " - help!!

pcc

Well-known Member
Joined
Jan 21, 2003
Messages
1,382
Office Version
  1. 2021
Platform
  1. Windows
I have an Access database that contains names of chemical compounds, and am using SQL (generated via VB in Excel) to add records to/extract records from the database.

Some compound contain single and double quotes on their names eg
2,2′,2″-Trihydroxytriethylamine

This is causing a problem in parsing the SQL since, if I use a single ' as a text delimiter, it clashes wiith the ' in the substance name, and I get an error, but if is use " as a delimiter I get the same problem:
Code:
strsql = "Select material from CAS_number where material =""" & substance & """;"
gives a problem because this resolves to
Code:
Select material from CAS_number where material ="2,2′,2″-Trihydroxytriethylamine"


Code:
strsql = "Select material from CAS_number where material ='" & substance & "';"
gives a problem because this resolves to
Code:
Select material from CAS_number where material ='2,2′,2″-Trihydroxytriethylamine'

Can anyone advise a way out of this conundrum?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Code:
strsql = "Select material from CAS_number where material =2,2'',2""-Trihydroxytriethylamine"
 
Upvote 0
That statement would fail as there is no " after the = sign...
 
Upvote 0
Code:
[FONT=Courier New]strsql = [COLOR=red][B]"[/B][/COLOR]Select material from CAS_number where [/FONT][FONT=Courier New]material = [COLOR=blue][B]'[/B][/COLOR]2,2[COLOR=blue][B]''[/B][/COLOR],2[B][COLOR=red]""[/COLOR][/B]-Trihydroxytriethylamine[COLOR=blue][B]'[/B][/COLOR][COLOR=black];[/COLOR][COLOR=red][B]"[/B][/COLOR][/FONT]

Double quotes are in red, single quotes are in blue.

Any good?
 
Upvote 0
Thanks for reply but the problem is not with the sql statement itself, it's how to generate it on-the-fly using VBA. I've decided for now to cheat a bit and simply replace " with $ in the substance name so that
2,2′,2″-Trihydroxytriethylamine

becomes

2,2′,2$-Trihydroxytriethylamine

I can then convert back during reporting to yield the original name. It's not ideal but I can't spend any more time worrying about it. Thanks for inputs anyhow....
 
Upvote 0
Yes, I'm aware the problem is with the VBA rather than the SQL - that's why I'm trying to correct the VBA.

If you place a breakpoint immediately after the strsql= statement and examine the contents of strsql in the Immediate window (Ctrl-G), it should be obvious where the problem lies. To do this, type ?strsql and hit Enter.

Did you try my code correction?
 
Upvote 0
Code:
strsql = "Select material from CAS_number where material ='" & replace(substance, "'", "''") & ";"

or you can probably also use (assuming the data doesn't have hashes in):
Code:
strsql = "Select material from CAS_number where material =#" & substance & "#;"
 
Upvote 0
Didn't try code as it will fail because

when
Select material from CAS_number where material = '2,2'',2""-Trihydroxytriethylamine';

is executed, there will be an imbalance in the quotes. this will be interpreted as
Select material from CAS_number where material = '2,2'
then there will be
',2""-Trihydroxytriethylamine'; after it, and this will cause an error

I appreciate your help but as I said I can't spend any more time on it as there are more pressing problems to resolve. I have parked it for now, substituting " with $ and this will do.

Thanks
 
Upvote 0
Rory can't get that to work either I'm afraid.
Please don't worry about it any more I don't want to waste any more time. I thought there might be a quick fix but it's not important enough to pursue any more.
Cheers
 
Upvote 0
May I politely suggest that you do actually try it.

Edit: I see you did. It (the first one)works for me using your sample.
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top