A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

"like" works as query, but not in code (Access 2003)



 
 
Thread Tools Display Modes
  #1  
Old September 19th, 2007, 01:03 PM posted to microsoft.public.access.queries
[email protected]
external usenet poster
 
Posts: 4
Default "like" works as query, but not in code (Access 2003)

In my Access application I'm fetching data thru SQL statements, and
not directly thru a RecordSet object. In my statements, such as
select count(*) as reccount from [Survey Info] where [Last Name] =
"Smith"
things work OK. The returned count is 1.

But if my statement is
select count(*) as reccount from [Survey Info] where [Last Name]
like "Smit*"
then I get back a reccount value, but the count is 0.

If I copy this SQL and use it in a query builder, the SQL performs
properly. It doesn't perform OK in my code.

Here is the context:
....
Dim conn As ADODB.Connection
Set conn = CurrentProject.Connection
Dim sql As String
sql = "select count(*) as reccount from [Survey Info] where [Last
Name] like "Smit*"
Dim recSet As New ADODB.Recordset
recSet.Open sql, conn, adOpenStatic

If recSet.RecordCount 0 Then
Dim filterCount As Long
filterCount = recSet.Fields("reccount").value
End If
....
I've searched groups and other web pages, and the help files, but
don't see any mention of a performance difference or error in Access.
It works the same (badly) on Windows XP and Vista. Again, I'm using
Access 2003 with a MDB file.

Thanks in advance,
Jerome

  #2  
Old September 19th, 2007, 01:45 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default "like" works as query, but not in code (Access 2003)

Try using "%" as the wildcard instead of "*".

SELECT Count(*) as reccount from [Survey Info] where [Last Name] like
"Smit%"

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..

wrote in message
oups.com...
In my Access application I'm fetching data thru SQL statements, and
not directly thru a RecordSet object. In my statements, such as
select count(*) as reccount from [Survey Info] where [Last Name] =
"Smith"
things work OK. The returned count is 1.

But if my statement is
select count(*) as reccount from [Survey Info] where [Last Name]
like "Smit*"
then I get back a reccount value, but the count is 0.

If I copy this SQL and use it in a query builder, the SQL performs
properly. It doesn't perform OK in my code.

Here is the context:
...
Dim conn As ADODB.Connection
Set conn = CurrentProject.Connection
Dim sql As String
sql = "select count(*) as reccount from [Survey Info] where [Last
Name] like "Smit*"
Dim recSet As New ADODB.Recordset
recSet.Open sql, conn, adOpenStatic

If recSet.RecordCount 0 Then
Dim filterCount As Long
filterCount = recSet.Fields("reccount").value
End If
...
I've searched groups and other web pages, and the help files, but
don't see any mention of a performance difference or error in Access.
It works the same (badly) on Windows XP and Vista. Again, I'm using
Access 2003 with a MDB file.

Thanks in advance,
Jerome



  #3  
Old September 19th, 2007, 02:58 PM posted to microsoft.public.access.queries
Jamie Collins
external usenet poster
 
Posts: 1,705
Default "like" works as query, but not in code (Access 2003)

On Sep 19, 1:45 pm, "John Spencer" wrote:
Try using "%" as the wildcard instead of "*".

SELECT Count(*) as reccount from [Survey Info] where [Last Name] like
"Smit%"


And if they want something to work in all Query Modes, they could try
the ALIKE operator too:

SELECT Count(*) as reccount from [Survey Info] where [Last Name] ALIKE
'Smit%';

Jamie.

--



  #4  
Old September 19th, 2007, 03:06 PM posted to microsoft.public.access.queries
[email protected]
external usenet poster
 
Posts: 4
Default "like" works as query, but not in code (Access 2003)

Oh, yeah. I knew that Access had its own versions of these (_ and %,
vs. % and *). But since the * worked in the stand-alone query I
thought that it would also behave in the ADO Recordset.

Your answer has been a very big help to me. Thank you.



On Sep 19, 7:45 am, "John Spencer" wrote:
Try using "%" as the wildcard instead of "*".

SELECT Count(*) as reccount from [Survey Info] where [Last Name] like
"Smit%"

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
.

wrote in message

oups.com...

In my Access application I'm fetching data thru SQL statements, and
not directly thru a RecordSet object. In my statements, such as
select count(*) as reccount from [Survey Info] where [Last Name] =
"Smith"
things work OK. The returned count is 1.


But if my statement is
select count(*) as reccount from [Survey Info] where [Last Name]
like "Smit*"
then I get back a reccount value, but the count is 0.


If I copy this SQL and use it in a query builder, the SQL performs
properly. It doesn't perform OK in my code.


Here is the context:
...
Dim conn As ADODB.Connection
Set conn = CurrentProject.Connection
Dim sql As String
sql = "select count(*) as reccount from [Survey Info] where [Last
Name] like "Smit*"
Dim recSet As New ADODB.Recordset
recSet.Open sql, conn, adOpenStatic


If recSet.RecordCount 0 Then
Dim filterCount As Long
filterCount = recSet.Fields("reccount").value
End If
...
I've searched groups and other web pages, and the help files, but
don't see any mention of a performance difference or error in Access.
It works the same (badly) on Windows XP and Vista. Again, I'm using
Access 2003 with a MDB file.


Thanks in advance,
Jerome



  #5  
Old September 19th, 2007, 04:34 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default "like" works as query, but not in code (Access 2003)

Good idea if they are using Access 2000 or later. ALIKE (as far as I know)
does not work in earlier versions.

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..

"Jamie Collins" wrote in message
ups.com...
On Sep 19, 1:45 pm, "John Spencer" wrote:
Try using "%" as the wildcard instead of "*".

SELECT Count(*) as reccount from [Survey Info] where [Last Name] like
"Smit%"


And if they want something to work in all Query Modes, they could try
the ALIKE operator too:

SELECT Count(*) as reccount from [Survey Info] where [Last Name] ALIKE
'Smit%';

Jamie.

--





  #6  
Old September 20th, 2007, 08:19 AM posted to microsoft.public.access.queries
Jamie Collins
external usenet poster
 
Posts: 1,705
Default "like" works as query, but not in code (Access 2003)

On Sep 19, 4:34 pm, "John Spencer" wrote:
ALIKE (as far as I know)
does not work in earlier versions.


Using a Jet 3.51 mdb, the ALIKE works fine for me in ADO using both
the Microsoft.Jet.OLEDB.3.51 and Microsoft.Jet.OLEDB.4.0 providers
respectively; also works fine for me in the Access97 interface (SQL
View of a Query object).

Jamie.

--


 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 12:44 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.