views:

493

answers:

2

Hi,

I have an Access 2003 database with some visual basic code using ADO calls in it. When I do a

strsql0 = "SELECT lnk_stockitm.C_C FROM lnk_stockitm WHERE (((lnk_stockitm.C_C) Like 'T*'));"
newRS.Open strsql0, cn1, adOpenKeyset, adLockReadOnly  
newRS.movelast

I get a '3021 either bof or eof is true or the current record has been deleted...' error. When I run the exact same query in the same function without the WHERE clause, like this

strsql0 = "SELECT lnk_stockitm.C_C FROM lnk_stockitm;

I get the correct result of 56,000 records. If I paste the full SQL statement with the WHERE clause into a regular query, like so

SELECT lnk_stockitm.C_C FROM lnk_stockitm WHERE (((lnk_stockitm.C_C) Like 'T*'));

it returns the correct subset of the results (2800 records).

Can anyone tell me what I am doing wrong, or what funny business Microsoft is playing?

Thanks.

+3  A: 

You need to use the '%' character as wildcard when using ADO.

MSDN Article: Using the Right Wildcard Characters in SQL Statements

Heath Hunnicutt
That is an excellent resource. Thanks.
Colin
+3  A: 

The wildcard difference is the cause for difference between what you execute from ADO and within your access database. Convert your statement to use "%" rather than "*". As a general rule of thumb, it may be a good idea to encapsulate your code by checking for eof before calling MoveLast. If your query has zero results it'll bomb out every time.

strsql0 = "SELECT lnk_stockitm.C_C FROM lnk_stockitm WHERE (((lnk_stockitm.C_C) Like 'T*'));"
newRS.Open strsql0, cn1, adOpenKeyset, adLockReadOnly  

if not newRs.eof then
   newRS.movelast
else
  ' do something here if necessary to handle blank results
end if
Jakkwylde
How embarrassing. I read about this a long time ago, and hadn't had a need to use it for so long I forgot.Thanks for the heads up on checking for EOF first. I'm actually doing that already. I just chopped out all the code to get to the minimum case causing my error, to make it quick and easy for people to look at.
Colin