Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with single quote in Word VBA SQL query?

Tags:

sql

ms-word

vba

I get a customer name from dropdown and use that value to query an excel spreadsheet, however, the name can contain a single quote (example: Adam's Meat). This breaks my application and how do I make a query with a variable that contains a single quote?

Private Sub cboCompany_Change()
            Dim customerName As String
            customerName = cboCompany.Value

rsT.Open "SELECT Customer, Postcode, Address1, Address2, State, Country FROM Customers WHERE  Customer = '" & customerName & "'", cn, adOpenStatic
like image 577
Morgan Avatar asked Oct 20 '10 05:10

Morgan


1 Answers

Where you specify two single quotes '', one will escape the other and will result in single, try to replace it like this:

customerName = Replace(customerName, "'", "''")
like image 159
Sarfraz Avatar answered Sep 22 '22 23:09

Sarfraz