Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Comparing Recordsets

Posted on 2004-09-29
9
Medium Priority
?
610 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
9 Comments
 
LVL 7

Accepted Solution

by:
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?
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
Technology Partners: 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

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!

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

885 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