Comparing Recordsets

Posted on 2004-09-29
Medium Priority
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
Question by:oagunbiade

Accepted Solution

SQL_Stu earned 500 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?
LVL 53

Expert Comment

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 ?

Author Comment

ID: 12179251
Yes i want to compare 3 fields to see if they are exactly the same as the previous rows 3 fields.
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!


Expert Comment

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.

Expert Comment

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
appstring_prev = appstring_next
appstring_next= rs(0) & rs(1) & rs(2)

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



LVL 18

Expert Comment

ID: 12211320
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.

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    

Expert Comment

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
      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
      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)

Expert Comment

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)


Author Comment

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

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.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

589 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