Solved

ado Recordset filtering

Posted on 2011-02-10
2
382 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
[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
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

622 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