SQL 使用 Like * 在 MS-Access 中有效,但在 VBA 中无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6037290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:36:28  来源:igfitidea点击:

Use of Like * Works in MS-Access but Not VBA

sqlvbams-accesssql-like

提问by Rich Estabrook

I have a simple query but am running into problems using LIKEin VBA. My SQL string in VBA is:

我有一个简单的查询,但LIKE在 VBA 中使用时遇到了问题。我在 VBA 中的 SQL 字符串是:

stsql1 = "Select Top 25 data.* from data where data.Description Like ('*') "

When I run this sql string in my VBA code I get no records returned, but if I copy/paste the same string into a query in SQL View in MS Access, the query returns the values I expect. Is there a trick to using the "Like" syntax in VBA?

当我在我的 VBA 代码中运行这个 sql 字符串时,我没有返回任何记录,但是如果我将相同的字符串复制/粘贴到 MS Access 的 SQL 视图中的查询中,该查询将返回我期望的值。在 VBA 中使用“Like”语法有技巧吗?

I can provide additional code and a small version of the database if that would help.

如果有帮助,我可以提供额外的代码和数据库的小版本。

回答by HansUp

For SQL, the database engine will accept either single or double quotes as text delimiters. So either of these 2 WHEREclauses will work.

对于 SQL,数据库引擎将接受单引号或双引号作为文本分隔符。所以这两个WHERE条款中的任何一个都可以。

WHERE some_field Like '*'
WHERE some_field Like "*"

VBA however only accepts double quotes as text delimiters, so you would have to use the second form.

然而,VBA 只接受双引号作为文本分隔符,因此您必须使用第二种形式。

Two other points about your SELECTstatement:

关于你的SELECT陈述的另外两点:

Select Top 25 data.* from data where data.Description Like ('*')
  1. TOP [number]is arbitrary without an ORDER BYclause
  2. You don't need parentheses surrounding your Likepattern ... you can use Like "*"
  1. TOP [number]没有ORDER BY条款是任意的
  2. 你不需要围绕你的Like模式的括号......你可以使用Like "*"

If your VBA code is using ADO with that SELECTstatement, you must change the wild card character from *to %...

如果你的VBA代码使用ADO与SELECT语句,必须通配符从变化*%...

WHERE data.Description Like '%'

回答by Jacob

In ADO/VBA, you have to use % instead of * as the wildcard. I ran into this a couple times in the past ....

在 ADO/VBA 中,您必须使用 % 而不是 * 作为通配符。我过去遇到过几次......

回答by rskar

Realize that there are at least 2 (yes two!) LIKE operators here.

意识到这里至少有 2 个(是的两个!) LIKE 运算符。

One is the LIKE operator of VBA.

一种是 VBA 的 LIKE 运算符。

The other is the LIKE operator of the SQL of the database you are attached to.

另一个是您所连接的数据库的 SQL 的 LIKE 运算符。

The usual wildcards in SQL are % (for any # of any characters) and _ (for one of any character).

SQL 中常用的通配符是 %(对于任何字符的任何 #)和 _(对于任何字符之一)。

Know also that MS Access can open databases that aren't Access; it could be Microsoft SQL Server, or Oracle or IBM DB2. (BTW, the database that is normal for Access is called Microsoft JET.) You may be sheltered from that truth when you create a Query object in Access - in that circumstance, you are using JET SQL even when it's a linked table you are querying.

还要知道 MS Access 可以打开不是 Access 的数据库;它可以是 Microsoft SQL Server、Oracle 或 IBM DB2。(顺便说一句,Access 的正常数据库称为 Microsoft JET。)当您在 Access 中创建 Query 对象时,您可能会避开这个事实 - 在这种情况下,您正在使用 JET SQL,即使它是您正在查询的链接表.

However, under VBA, when using either DAO or ADO, you're talking directly to whatever the database system happens to be, in which case you MUST use the SQL of that specific system.

但是,在 VBA 下,当使用 DAO 或 ADO 时,您直接与数据库系统进行对话,在这种情况下,您必须使用该特定系统的 SQL。

OK, short answer: Use % like cularis said.

好的,简短的回答:像 cularis 所说的那样使用 %。

回答by Steven

I can't add a comment, but I think it would be worth noting that you have to use %even if you are querying MS Access.

我无法添加评论,但我认为值得注意的是,%即使您正在查询 MS Access ,您也必须使用。

(example: Outlook VBA runs query on an Access database. The proper query is select * where user like '%bob%', even though this query would not work if plugged directly into an MS Access query).

(例如:Outlook VBA 在 Access 数据库上运行查询。正确的查询是select * where user like '%bob%',即使如果直接插入 MS Access 查询,此查询将不起作用)。