Word VBA SQL查询中如何处理单引号? [英] How to deal with single quote in Word VBA SQL query?

查看:338
本文介绍了Word VBA SQL查询中如何处理单引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从下拉列表中获得一个客户名称,并使用该值查询excel电子表格,但是,该名称可以包含单引号(例如:Adam's Meat).这会破坏我的应用程序,并且如何使用包含单引号的变量进行查询?

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

推荐答案

在指定两个单引号''的地方,一个将转义另一个,并导致产生单引号,请尝试如下替换它:

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, "'", "''")

这篇关于Word VBA SQL查询中如何处理单引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆