简体   繁体   English

在* MS-Access但不是VBA中使用Like * Works

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

I have a simple query but am running into problems using LIKE in VBA. 我有一个简单的查询,但在VBA中使用LIKE遇到了问题。 My SQL string in VBA is: 我在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. 当我在我的VBA代码中运行此sql字符串时,我没有返回任何记录,但是如果我将相同的字符串复制/粘贴到MS Access中的SQL视图中的查询中,则查询将返回我期望的值。 Is there a trick to using the "Like" syntax in VBA? 在VBA中使用“Like”语法有诀窍吗?

I can provide additional code and a small version of the database if that would help. 如果有帮助,我可以提供额外的代码和数据库的小版本。

For SQL, the database engine will accept either single or double quotes as text delimiters. 对于SQL,数据库引擎将接受单引号或双引号作为文本分隔符。 So either of these 2 WHERE clauses will work. 因此,这两个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 SELECT statement: 关于SELECT语句的另外两点:

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

If your VBA code is using ADO with that SELECT statement, you must change the wild card character from * to % ... 如果您的VBA代码在该SELECT语句中使用ADO,则必须将通配符从*更改为% ...

WHERE data.Description Like '%'

In ADO/VBA, you have to use % instead of * as the wildcard. 在ADO / VBA中,您必须使用%而不是*作为通配符。 I ran into this a couple times in the past .... 我过去碰到过几次......

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查询时也不起作用)。

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; 还要知道MS Access可以打开非Access的数据库; it could be Microsoft SQL Server, or Oracle or IBM DB2. 它可以是Microsoft SQL Server,也可以是Oracle或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. (顺便说一句,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说的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM