Solved

Sum / Filter on ADO Recordset

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

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 27

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now