We help IT Professionals succeed at work.
Get Started

Handle Apostrophes in SQL Parameter

Chi Is Current
on
315 Views
Last Modified: 2017-05-05
The following query works fine, as long as there are no apostrophes in field value: tblDistributors.distParentCo  --  ex: "D'Angelo"
SQL = "TRANSFORM Round(Sum(tblDepletions_DETAIL.detQuan),0) AS Sales"
SQL = SQL + " SELECT tblProducts_DEPL.DprodBrand, tblProducts_DEPL.DprodLabel, tblDepletions_DETAIL.detFormat, tblProducts_DEPL.DprodVarietal"
SQL = SQL + " FROM (tblDistributors INNER JOIN tblDepletions ON tblDistributors.distDistID = tblDepletions.depDistID) INNER JOIN (tblProducts_DEPL INNER JOIN tblDepletions_DETAIL ON tblProducts_DEPL.DprodZSKU = tblDepletions_DETAIL.detZSKU) ON tblDepletions.depID = tblDepletions_DETAIL.detDeplID"
SQL = SQL + " WHERE (((tblDistributors.distParentCo)=[forms]![frmRPT_COMP_PntDepl_SF]![cmbDist]) AND ((tblDepletions.depDate) Between [forms]![frmRPT_COMP_PntDepl_SF]![BegRollDT] And [forms]![frmRPT_COMP_PntDepl_SF]![EndRollDT]))"
SQL = SQL + " GROUP BY tblProducts_DEPL.DprodBrand, tblProducts_DEPL.DprodLabel, tblDepletions_DETAIL.detFormat, tblProducts_DEPL.DprodVarietal ORDER BY tblProducts_DEPL.DprodBrand, tblProducts_DEPL.DprodLabel, tblDepletions_DETAIL.detFormat, tblProducts_DEPL.DprodVarietal"
SQL = SQL + " PIVOT DateDiff("m",[forms]![frmRPT_COMP_PntDepl_SF]![BegRollDT]-1,[depDate]) In (1,2,3,4,5,6,7,8,9,10,11,12);"

Open in new window


I tried adding a 'Replace' function in Line #4, which does not work.  Error: This expression is typed incorrectly, or is too complex to be evaluated...":
SQL = "TRANSFORM Round(Sum(tblDepletions_DETAIL.detQuan),0) AS Sales"
SQL = SQL + " SELECT tblProducts_DEPL.DprodBrand, tblProducts_DEPL.DprodLabel, tblDepletions_DETAIL.detFormat, tblProducts_DEPL.DprodVarietal"
SQL = SQL + " FROM (tblDistributors INNER JOIN tblDepletions ON tblDistributors.distDistID = tblDepletions.depDistID) INNER JOIN (tblProducts_DEPL INNER JOIN tblDepletions_DETAIL ON tblProducts_DEPL.DprodZSKU = tblDepletions_DETAIL.detZSKU) ON tblDepletions.depID = tblDepletions_DETAIL.detDeplID"
SQL = SQL + " WHERE (((tblDistributors.distParentCo)=Replace([forms]![frmRPT_COMP_PntDepl_SF]![cmbDist],"'","''")) AND ((tblDepletions.depDate) Between [forms]![frmRPT_COMP_PntDepl_SF]![BegRollDT] And [forms]![frmRPT_COMP_PntDepl_SF]![EndRollDT]))"
SQL = SQL + " GROUP BY tblProducts_DEPL.DprodBrand, tblProducts_DEPL.DprodLabel, tblDepletions_DETAIL.detFormat, tblProducts_DEPL.DprodVarietal ORDER BY tblProducts_DEPL.DprodBrand, tblProducts_DEPL.DprodLabel, tblDepletions_DETAIL.detFormat, tblProducts_DEPL.DprodVarietal"
SQL = SQL + " PIVOT DateDiff("m",[forms]![frmRPT_COMP_PntDepl_SF]![BegRollDT]-1,[depDate]) In (1,2,3,4,5,6,7,8,9,10,11,12);"

Open in new window

'Would appreciate your ideas for a solution!

Many Thanks! ~ Jacob

P.S.  Stepping away for a bit.
Comment
Watch Question
Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007
Commented:
This problem has been solved!
Unlock 4 Answers and 16 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE