Solved

Comparing Recordsets

Posted on 2004-09-29
9
588 Views
Last Modified: 2013-12-25
I want to compare the last two rows in a table and im trying to do this by creating two recordsets containing the same data. Is there a better way to do this? And is there a way to compare the recordsets as a whole instead of comparing the fields in the recordsets eg if Recordset1 = Recordset2. Thanks
0
Comment
Question by:oagunbiade
[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
9 Comments
 
LVL 7

Accepted Solution

by:
SQL_Stu earned 125 total points
ID: 12178616
You could use Recordset.Clone to mirror your original recordset.

Perhaps you could join all the fields together into 1 string and then compare the whole record against the clone?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 12179197
What exactly do you need to know ?
Do you want to know if there are more records in one table (or recordset) than in the other ?
Or do you want to compare at field level to see if the data is exactly the same ?
0
 

Author Comment

by:oagunbiade
ID: 12179251
Yes i want to compare 3 fields to see if they are exactly the same as the previous rows 3 fields.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Arundel_Castle
ID: 12185673
What database are you using. You could write an SQL statement to do this for you then just fit it into a function call in your code. Tell me what database.
0
 
LVL 3

Expert Comment

by:sgayatri
ID: 12188442
You can use a single ADO recordset  to do this
kount = 0  '' your row count
str1= "select f1,f2,f3 from yourdb"

rs.open str1,cn,adopenkeyset  'Where cn is your ado connection
if rs.eof = false then
appstring_next= rs(0) & rs(1) & rs(2)
appstring_prev = "" 
end if
while rs.eof = false
  kount = kount + 1
  if appstring_prev = appstring_next then
     msgbox kount  & " and " & kount -1 & " rows are similar " 
 end if
rs.movenext
appstring_prev = appstring_next
appstring_next= rs(0) & rs(1) & rs(2)
wend

Infact this code will find out any two adjacent rows having two fields as similar ....

 

 



0
 
LVL 18

Expert Comment

by:JR2003
ID: 12211320
oagunbiade,
I agree with Arundel_Castle you should do this with an sql statement. something like below. Not that both tables in the from statement are the same table with different aliases.
If the result is 0 (zero) then the last 3 fields don't match.
JR

select count (*)
from Table1 as A, table1 as B
where A.fieldPK = 'XYZ'
   and B.fieldPK = 'XYY'
   and A.field1 = B.field1    
   and A.field2 = B.field2    
   and A.field3 = B.field3    
0
 
LVL 5

Expert Comment

by:Pi7
ID: 12235754
oagunbiade, did you say you want to compare the LAST two rows? If so then you can do it in fewer steps using one recordset
Dim Match as Boolean
Dim LastRow as String
Dim PrevRow As String

With rs   'your recordset
      .MoveLast
      LastRow= Cstr(.Fields(0)) & " " & Cstr(.Fields(1)) & " " & Cstr(.Fields(2))   'concatenates the values in the first 3 fields and assigns it to a string variable
      .MovePrevious
      PrevRow =Cstr(.Fields(0)) & " " & Cstr(.Fields(1)) & " " & Cstr(.Fields(2))   'concatenates the values in the first 3 fields and assigns it to a string variable
End With

Match=( Instr(1,LastRow,PrevRow)>0)
   
0
 
LVL 5

Expert Comment

by:Pi7
ID: 12235763
Of course before the code I submited you should select all the records in the table(either by using the ado control at design time or using the sql select statement,which I prefer)

regards,
Pi7
0
 

Author Comment

by:oagunbiade
ID: 12246952
Thanks everyone for your contribution but the clone method seems to work perfectly.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…

734 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