tags:

views:

513

answers:

6

I don't do a lot of coding with VB6, but I'm updating an existing app now and just encountered a snag.

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.

I'm querying the same database - (it's in MS Access).

When querying from within MS Access, the following query works:

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

when I build that query in VB6, I have to do this:

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

What's happening? Is that normal?

A: 

I've never seen the asterisk character used as a wildcard for a like statement (just everywhere else) -- generally speaking the percentage sign is what you would need to use.

Andrew G. Johnson
It is actually official in Accesshttp://office.microsoft.com/en-us/access/HP010322531033.aspxWithin access, you have to use it, as far as I remember.
Uri
replacing * with % within access results in 0 records found. I'm pretty sure the same thing happens when querying the same database from a vb.net app. I always have to use the *. This is the only case I've found with access where the % actually works. but I too have heard that % is required
42
Not ACCESS, but JET.
David-W-Fenton
@Uri: you remember wrong :) The Access UI can indeed use the % wildcard characters. See http://office.microsoft.com/en-us/access/HP030704831033.aspx.
onedaywhen
@David W. Fenton: you forgot about ACE again, silly you :)
onedaywhen
+5  A: 

Access used to have its own incompatible version of SQL, so I think it uses the * for legacy reasons.

When you use VB6 you usually use ODBC and a more standardized SQL, so the more common wildcards apply. Remember that VB6 doesn't care which DB you use, so if you used something else (e.g., SQL server) it would probably only understand the percentage signs.

I am guessing that the Access-ODBC connector converts things for you.

Uri
good point. you're probably right.
42
When I was in high school I used to work at fixing problems that people were having in access or in migrating away from access. It's incompatible SQL was one of the main issues.
Uri
Access does not have its own SQL, but Jet does. It uses * and ? for wildcards, whereas most databases use % and _ for the same purposes. If you're using Access 2003 or later you can set them to use ANSI 92 mode, and you'll be able to use % and _ within Access.
David-W-Fenton
"Access used to have its own incompatible version of SQL" -- incompatible with what? Used to? Despite the name, ANSI-92 Query Mode remains significanly incompatible with both the ANSI/ISO SQL-92 Standard and all versions of T-SQL.
onedaywhen
This also applies to using Access queries with ADO through command type adCmdTable -- I spent a few hours going crazy because the query looked fine in Access but had an extra 190 rows in my VB program, thanks to a LIKE clause in one of the queries that were joined to form it.
peejaybee
A: 

Yeah, that's normal.

I think its the difference between DAO (what Access uses internally), and ADO (what VB6 uses to talk to Access).

BradC
No, it's actually the difference between ANSI-89 Query Mode (which DAO always uses) and ANSI-92 Query Mode (which ADO always uses). Access can use either depending on which is currently set for the UI.
onedaywhen
+4  A: 

Access will use a subset of ANSI-89 wildcards by default, VB6, connecting through ADO will use ANSI-92.

Operator Comparison

Changing the mode Access uses

cmsjr
I actually like this one better than my own (accepted) answer.
Uri
Why, thank you @Uri...
cmsjr
it's tough to pick only one answer. we need an option for saying 'the following answers were correct, AND helpful'.
42
+1  A: 

I don't know if this applies to VB6, but within Access, you can use

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.

HansUp
Yes, it does apply regardelss of how or what is used to conenct to ACE/Jet.
onedaywhen
A: 

Yes, you can get away with ALIKE in a Jet 4.0 OLE DB inquiry (i.e. from VB6 using 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.

Bob