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

查看:27
本文介绍了如何处理 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天全站免登陆