sqlvbaexcel

Dealing with single quotes in excel vba sql queries


I am using excel sql queries to fetch data from the backend according to the selection i make in a dropdown box.However my sql queries are not working in cases where there is a single quote(') in the dropdown box value. For example-- Men's Wear . Please suggest me a way to deal with this problem

    SQL = "select Segment,Weeks,Value from [Category performance data$] where       Departments='" &       Sheet3.Range("Dept_name").Value & _
     "' and Metric ='Total Spend' group by Segment,Weeks,Value"
    Call execQry   

Solution

  • Departments='" & replace(Sheet3.Range("Dept_name").Value, "'", "''") & _ "' and Metric