Solved

ado Recordset filtering

Posted on 2011-02-10
2
375 Views
Last Modified: 2012-05-11
Hi,
I have using a query to open a recordset where I have three fields namely Amount, TransNo, TransDate. When I open the recordset in the win form  I will have to loop through this recordset and compare each of the amount against the value user entered.
i.e. If RS.Fileds("Amount") <= txtAmount Then.....

How do I loop through this in ADO Recordset as this can be done only in VB script? Moreover, there might be occasion where multiple records might meet the criteria in that case I need to requery the recordset and select the vary latest one using the transdate field as filtering criteria.
i.e.
Amount        TransDate                transNo
29                  12/01/2011             RT2568
20                   05/01/2011            QR2148    
45                   01/02/2011             RY2531
Say, If I use Criteria If Amount <= 50 Then all three will be qulifying this criteria so I willl select the first one because that is the latest one in this recordset.

Please someone help me?  
0
Comment
Question by:ube100
2 Comments
 
LVL 1

Accepted Solution

by:
Srinivasan__ earned 500 total points
ID: 34868673
The vbScript routine will search for the value and the recordset.

Pass the query with the order by for TransDate column with the Descending order
Set objCon = Server.CreateObject("ADODB.Connection")
Set ObjRS = Server.CreateObject("ADODB.Recordset")


ObjCon.Open Connection_String
ObjRs.Open <sqlstatement>,objCon,3,4

3,4 - adOpenKeySet, adLockPessimistic

sub CheckRecordSet(objRS, Value)
    If NOT ObjRS.BOF OR objRS.EOF Then 
    While NOT objRS.EOF
          If objRS.Fields("Amount")<=Value)
           -- Do Something --
          End If
    Wend
    End If
End Sub

Query : Select Amount, TransDate, TransID from <table> order by TransDate DESC

Open in new window

0
 
LVL 16

Expert Comment

by:AlexPace
ID: 34872312
I would seriously consider changing the SQL statement to only return the valid data. You could use a subquery to select only the max date with a matching amount.  This will return a single date.  Then that date is used in the WHERE criteria of your outer query, so it only returns records with the requested amount on the max date. For good measure sort them descending by transaction number.  This way the first record in your recordset will always be the one you were looking for and if you happen to have any others in the recodset they will be others with the same amount also on the same day so you can show them or not.
MySQL = "SELECT * FROM TheTable " & _
        "WHERE Amount = " & txtAmount & _
        " AND TransDate = (" & _
        "SELECT MAX(TransDate) FROM TheTable " & _
        "WHERE Amount = " & txtAmount & ") " & _
        "ORDER BY transNo DESC"

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 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

20 Experts available now in Live!

Get 1:1 Help Now