Solved

Sum / Filter on ADO Recordset

Posted on 2006-05-30
5
2,689 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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

840 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