简体   繁体   English

MS Access 查询:为什么从 VB6 应用程序调用 LIKE 时行为不同?

[英]MS Access query: why does LIKE behave differently when being called from VB6 app?

I don't do a lot of coding with VB6, but I'm updating an existing app now and just encountered a snag.我不会用 VB6 进行大量编码,但我现在正在更新现有的应用程序,但遇到了一个障碍。

I figured out the problem.我解决了这个问题。 In VB6, queries must use the % wild card when using LIKE , but in MS Access, you have to use the ***** wild card .在 VB6 中,查询在使用LIKE时必须使用%通配符,但在 MS Access 中,您必须使用 *****通配符

I'm querying the same database - (it's in MS Access).我正在查询同一个数据库 - (它在 MS Access 中)。

When querying from within MS Access, the following query works:从 MS Access 中查询时,以下查询有效:

SELECT * FROM table WHERE field LIKE '*something*'

when I build that query in VB6, I have to do this:当我在 VB6 中构建该查询时,我必须这样做:

SELECT * FROM table WHERE field LIKE '%something%'

What's happening?发生了什么? Is that normal?这是正常的吗?

Access used to have its own incompatible version of SQL, so I think it uses the * for legacy reasons. Access 曾经有自己不兼容的 SQL 版本,所以我认为它使用 * 是出于遗留原因。

When you use VB6 you usually use ODBC and a more standardized SQL, so the more common wildcards apply.当您使用 VB6 时,您通常使用 ODBC 和更标准化的 SQL,因此更常见的通配符适用。 Remember that VB6 doesn't care which DB you use, so if you used something else (eg, SQL server) it would probably only understand the percentage signs.请记住,VB6 并不关心您使用哪个数据库,因此如果您使用其他东西(例如,SQL 服务器),它可能只会理解百分比符号。

I am guessing that the Access-ODBC connector converts things for you.我猜测 Access-ODBC 连接器会为您转换内容。

Access will use a subset of ANSI-89 wildcards by default, VB6, connecting through ADO will use ANSI-92.默认情况下,Access 将使用 ANSI-89 通配符的子集,VB6,通过 ADO 连接将使用 ANSI-92。

Operator Comparison 运算符比较

Changing the mode Access uses更改 Access 使用的模式

I don't know if this applies to VB6, but within Access, you can use我不知道这是否适用于 VB6,但在 Access 中,您可以使用

ALIKE '%something%'

and the % characters will be treated as wildcards regardless of whether you're using VBA with DAO or ADO, or creating a query in the query editor.并且 % 字符将被视为通配符,无论您是在 DAO 或 ADO 中使用 VBA,还是在查询编辑器中创建查询。

我从未见过星号字符用作 like 语句的通配符(仅在其他任何地方)——一般来说,百分号是您需要使用的。

Yeah, that's normal.是的,这很正常。

I think its the difference between DAO (what Access uses internally), and ADO (what VB6 uses to talk to Access).我认为它是 DAO(Access 在内部使用的)和 ADO(VB6 用来与 Access 对话的)之间的区别。

Yes, you can get away with ALIKE in a Jet 4.0 OLE DB inquiry (ie from VB6 using ADO):是的,您可以在 Jet 4.0 OLE DB 查询中使用 ALIKE(即从 VB6 使用 ADO):

JeTTY version 0.5.68
>open booksale.mdb;
#Opened database booksale.mdb (Jet3X "97")
>select * from authors where author like "ba*";
#No rows to display
>select * from authors where author like "ba%";
               Page 1 of 1
Au_ID Author     Year Born
───── ────────── ─────────
10    Bard, Dick 1941
>select * from authors where author alike "ba%";
               Page 1 of 1
Au_ID Author     Year Born
───── ────────── ─────────
10    Bard, Dick 1941
>

Of course you gain compatibility with Access but then lose ANSI SQL-92 compatibility for later upsizing to SQL Server, etc. and ultimately make more work for yourself.当然,您获得了与 Access 的兼容性,但随后失去了ANSI SQL-92 兼容性,以便以后升级到 SQL Server 等,并最终为自己做更多的工作。

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

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