[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Query Returning Wrong Number of Rows (ADO)

Posted on 2006-05-22
11
Medium Priority
?
301 Views
Last Modified: 2010-05-01
I have something like the following. The connection is already set. If I copy the strSql in debug and make a query in Access, it returns 15 rows. When I run it in the program, it returns 1 row. Hoping it's something obvious that I'm overlooking:

-----
   Dim cnAccounting As ADODB.Connection
   Dim rsFilter As ADODB.Recordset
   Dim strSql As String

   'cnAccounting is already set
   strSql = "Some query that works fine in Access"
   Set rsFilter = New ADODB.Recordset
   rsFilter.Open strSql, cnAccounting, adOpenKeyset, adLockReadOnly
-----
0
Comment
Question by:GivenRandy
11 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16736816
Hi GivenRandy,

Set your CursorLocation to adUseClient

Cheers!
0
 
LVL 9

Author Comment

by:GivenRandy
ID: 16736985
No change. :(
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 400 total points
ID: 16737000
what if you try this:
rsFilter.Open strSql, cnAccounting
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Author Comment

by:GivenRandy
ID: 16737047
RecordCount = -1

(instead of 1)
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16737191
what is your current code?

should have:
   Set rsFilter = New ADODB.Recordset
rs.cursorlocation = aduseclient
   rsFilter.Open strSql, cnAccounting
0
 
LVL 9

Author Comment

by:GivenRandy
ID: 16737652
Yep, got this code and still only one row:

   Set rsFilter = New ADODB.Recordset
   rsFilter.CursorLocation = adUseClient
   rsFilter.Open strSQL, cnAccounting
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16737722
I haven't use ADO for a long time.

What about:
 rsFilter.Open strSQL, cnAccounting, adOpenStatic

Can you also show your ConnectionString? Have you set any properties on the Connection object? Are you really sure that your strSQL is valid?
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 400 total points
ID: 16737735
0
 
LVL 18

Expert Comment

by:Sethi
ID: 16738933
Counting through a recordset is not an optimized way of doing things. Instead fire a count sql query and get the count from the database and then write you original sql query.

strSql = "Select count(*) from YourTable"
   Set rsFilter = New ADODB.Recordset
   rsFilter.Open strSql, cnAccounting, adForwardOnly, adLockReadOnly
lngCount=Clng(rsFilter(0))

strSql = "Some query that works fine in Access"
   Set rsFilter = New ADODB.Recordset
   rsFilter.Open strSql, cnAccounting, adOpenKeyset, adLockReadOnly
0
 
LVL 6

Accepted Solution

by:
junglerover77 earned 1600 total points
ID: 16738989
I think that's because you used * as the wildchar in your query string. You need to change it to % .

For example, in Access:
Select contact_id, contact_name from Contact where contact_name like 'J*'

It will return all contacts whose name starts with J. But in ADO, you Must change it like this:
Select contact_id, contact_name from Contact where contact_name like 'J%'

Regards,
Jungle
0
 
LVL 9

Author Comment

by:GivenRandy
ID: 16741726
Yep, changing the "*" to "%" did it. Had been using ADO for SQL Server and copy-and-paste to Access returned "wrong" answer. That query works fine when put as a query in the Access database itself, but not from ADO. Works fine with ADO to SQL Server. Strange. Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question