Solved

Sum / Filter on ADO Recordset

Posted on 2006-05-30
5
2,797 Views
Last Modified: 2013-11-23
I have an ADO recordset that I later apply a Filter to. Now, can't I also do a Sum on individual columns in that filtered recordset? How?
0
Comment
Question by:GivenRandy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 16793640
you have to do that with code.

now, I had worked in my early projects with filtered recordsets, and meanwhile found out that running the (amended) query on the database will be at least as fast as doing filtering etc... the bigger the main recordset, the faster the results by running the query instead of filtering the recordset
0
 
LVL 6

Expert Comment

by:junglerover77
ID: 16795502
With ADO 2.7 or higher versions, you can apply a filter to a Recordset. Here is the sample code:

http://msdn.microsoft.com/library/en-us/ado270/htm/mdprofilterx.asp

After a filter is applied to a recordset, you can just loop through the filtered recordset and do a sum on any columns as you want.

0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 16795594
do a looping. for example.

dim sum

rs.filter = "fldname = `Joseph`"

do until rs.eof
    sum = sum + rs!age
    rs.movenext
loop


the example will add all the age whose name is Joseph.

0
 
LVL 1

Expert Comment

by:dwe0608
ID: 16796284
you can also use an SQL passthrough query (using ADO), which is in essence querying the same table twice with different parameters - if you post the sql query you are using I'll see if I can knock it out for you ...
0
 
LVL 28

Expert Comment

by:Ark
ID: 16796304
You can use SQL statement for both sum and filter:

'Assume you have a table "OrdersTable" with fields
'"Prices", "CustomerID" and "CustomerName"

Dim strSQL As StringDim rst As ADODB.Recordset

strSQL = "SELECT SUM(Prices) As mySum, COUNT(CustomerID) As myCount FROM OrdersTable WHERE CustomerName =`Joseph`"
'mCN is connection name
Set rst = mCN.Execute(strSQL, , adCmdText)
Debug.Print "Sum (for Joseph)  = " & rst("mySum")
Debug.Print "Count (for Joseph) = " & rst("myCount")
rst.Close
Set rst = Nothing
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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
Course of the Month9 days, 19 hours left to enroll

623 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