473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Compare data between two recordsets / lookup recordset value in another recordset

18
As the title suggets, I have created to recordsets (1: current year, 2: previous year)
I simply want to be able to do the following:
1-by-1 lookup an id/value in last year and pull the information back displaying it with Debug.Print.

from there I think i know how to write this data to an array.
Jun 27 '13 #1

✓ answered by ADezii

If I am reading you correctly, we can make use of a Boolean Variable to let us know whether or not any Match(es) were found for each Record in the 2013 Data.
Expand|Select|Wrap|Line Numbers
  1. Dim blnMatchFound As Boolean
  2.  
  3. '********************** Code Intentionally omitted **********************
  4. With rst
  5.   Do While Not .EOF                         'Loop thru ALL in tbl2013
  6.     Do While Not rst1.EOF                   'Loop thru ALL in tbl2012
  7.       If ![FirmID] = rst1![FirmID] Then     'Match foound on [FirmID]
  8.         blnMatchFound = True                'At least 1 Match Found
  9.           Debug.Print ![FirmID]               'Print relvant 2013 Data
  10.           Debug.Print ![Perc]                 '           "
  11.           Debug.Print ![Turnover]             '           "
  12.           Debug.Print ![Total]                '           "
  13.           Debug.Print ![FirmName]             '           "
  14.  
  15.           Debug.Print rst1![FirmID]           'Print relvant 2012 Data
  16.           Debug.Print rst1![FirmName]         '           "
  17.           Debug.Print rst1![Perc]             '           "
  18.       End If
  19.         rst1.MoveNext                       'Next Record in 2012
  20.     Loop
  21.       'No Match(es) found for Iteration, print 2013 Data only
  22.       If Not blnMatchFound Then
  23.         Debug.Print ![FirmID], ![FirmName], ![Perc], ![Turnover], ![Total]
  24.       Else
  25.         'Match(es) found, must RESET for next Iteration of 2013 Data
  26.         blnMatchFound = False
  27.       End If
  28.        rst1.MoveFirst                       'Move to First Record in 2012,
  29.                                             'Getting ready for next Iteration
  30.       .MoveNext                             'Next Record in 2013
  31.   Loop
  32. End With
  33. '********************** Code Intentionally omitted **********************
  34.  

17 7851
rizo98
18
the code I currently have is as follows:

Expand|Select|Wrap|Line Numbers
  1. Function arraytest2()
  2.  Dim db As DAO.Database
  3.  Dim rst As DAO.Recordset
  4.  Dim rst1 As DAO.Recordset
  5.  Dim rst1filtered As DAO.Recordset
  6.  
  7.  Dim Rowcount As Long
  8.  Dim FieldCount As Integer
  9.  Dim i As Integer
  10.  Dim j As Integer
  11.  Dim arrCount() As Variant
  12.  
  13.  Set db = CurrentDb
  14.  
  15.  
  16.  
  17.  Set rst = db.OpenRecordset("qry_Market Share", dbOpenDynaset)
  18.  Set rst1 = db.OpenRecordset("qry_Market Share1", dbOpenDynaset)
  19.  Set rst1filtered = db.OpenRecordset("qry_Market Share1", dbOpenDynaset)
  20.  
  21.  
  22. Rowcount = rst.RecordCount
  23. FieldCount = rst.Fields.count
  24.  
  25. 'set the dimensions for the array
  26. ReDim arrCount(0 To Rowcount, 0 To 8)
  27.  
  28.  
  29. rst.MoveLast
  30. rst.MoveFirst
  31.  
  32.  
  33. Do Until rst.EOF
  34.  
  35. rst1filtered.Filter = "Firmid = " & rst.Fields("Firmid").Value & ""
  36. Set rst1filtered = rst1.OpenRecordset
  37.  
  38.  
  39.  
  40.         'write to the array
  41.         arrCount(i, 0) = rst.Fields("FirmID")
  42.         arrCount(i, 1) = rst.Fields("FirmName")
  43.         arrCount(i, 2) = rst.Fields("Perc")
  44.         arrCount(i, 3) = rst.Fields("Turnover")
  45.         arrCount(i, 4) = rst.Fields("Total")
  46.         arrCount(i, 5) = rst1filtered.Fields("FirmID").Value
  47.         arrCount(i, 6) = rst1filtered.Fields("FirmName").Value
  48.         arrCount(i, 7) = rst1filtered.Fields("perc").Value
  49.  
  50.  
  51.             rst.MoveNext
  52.         'see the array in the Immediate window
  53.         Debug.Print rst.Fields("FirmID"), rst.Fields("FirmName"), rst.Fields("Perc"), rst1.Fields("FirmID"), rst1.Fields("FirmName"), rst1.Fields("perc")
  54.          'Debug.Print i, arrCount(i, 0), arrCount(i, 1), arrCount(i, 2), arrCount(i, 3), arrCount(i, 4), arrCount(i, 5), arrCount(i, 6), arrCount(i, 7)
  55.  
  56.         i = i + 1
  57. Loop



End Function
Jun 27 '13 #2
ADezii
8,834 Expert 8TB
Unless I am misinterpreting your request, create a Recordset directly from the Filter and display the results, as in:
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rst As DAO.Recordset
  3. Dim strSQL As String
  4. Dim intNumOfFields As Integer
  5. Dim intFldCtr As Integer
  6.  
  7. 'Fictitious Value to search for
  8. Const conVALUE_TO_SEARCH As Long = 1
  9.  
  10. strSQL = "SELECT * FROM tblPreviousYear WHERE [ID] = " & conVALUE_TO_SEARCH
  11.  
  12. Set MyDB = CurrentDb
  13. Set rst = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
  14.  
  15. rst.MoveLast: rst.MoveFirst     'For accurate Record Count
  16.  
  17. intNumOfFields = rst.Fields.Count
  18.  
  19. With rst
  20.   Do Until .EOF       'Return anything?
  21.     For intFldCtr = 0 To intNumOfFields - 1
  22.       Debug.Print .Fields(intFldCtr) & " | " & .Fields(intFldCtr).Value
  23.     Next
  24.       Debug.Print "--------------------------------------------------"      'Separator Bar
  25.         .MoveNext
  26.   Loop
  27. End With
  28.  
  29. rst.Close
  30. Set rst = Nothing
Jun 27 '13 #3
rizo98
18
Thanks for the reply. I want to search another recordset or array though. let me give you an example. I have a recordset called Athlete2013 and another recordset called Athlete 2012.
Looking at the below recordsets, I want to take Mo Farah from Recordset_Athlete2013 and search for him in Recordset_Athlete2012. Having found him, I want to to display his position. So as a final output I will have something like below. I hope this is clear. I want to be able to do this for each record in Recordset_Athlete2013.

Expand|Select|Wrap|Line Numbers
  1. Athlete    2013_position    2012_Position
  2. Mo Farah    1    2
  3.  
  4.  
  5.  
  6.  
  7. Recordset_Athlete2013
  8. Athlete    2013_position
  9. Mo Farah    1
  10.  
  11. Recordset_Athlete2012
  12. Athlete    2012_Position
  13. Mo Farah    2
Jun 27 '13 #4
Rabbit
12,516 Expert Mod 8TB
If that's the result you wnat, there's no need for any VBA code. Just join the two recordsets on the Athlete.
Jun 27 '13 #5
rizo98
18
But if I just join the recordsets on the athletes then will that not jumble up the results? E.g. Mo Farah was the winner in 2013 and runner up in 2012. If I joined the recordsets would I not have Mo Farah's 2013 score and someone else's (2012 winner) score next to it?
ASo I would have as below. But Mo was in 2nd place in 2012 and not 1st.
Athlete 2013_position 2012_Position
Mo Farah 1 1


Also the recordsets do not have the same number of records.
Jun 27 '13 #6
Rabbit
12,516 Expert Mod 8TB
Why would it jumble? As long as you join on the key fields, in this case Athlete, then the records will match up. If the recordsets don't have the same number of records, you can just use an outer join, be it left, right, or full outer.
Jun 27 '13 #7
zmbd
5,501 Expert Mod 4TB
also sounds like the database isn't normalzied...> Database Normalization and Table Structures.
Jun 27 '13 #8
ADezii
8,834 Expert 8TB
@zmbd:
I feel as though you are correct about the Data not being Normalized.

I think the question here is Does each Athlete have a Unique Identifier that is consistent in each Table?
Jun 27 '13 #9
rizo98
18
I can do this by creating a query but i need to do it using vba. So i need to know the best method of going about it. By using recordsets, arrays or another method. The database is normalised. I have gathered the data for 2012 and 2013 using queries. I can create another query and do a right join on 2012 and get all corresponding records for 2012. However as i said i need to do this in vba. I have assigned 2013 query to a recordset called rst and 2012 query to another recordset called rst1 as shown in my code above. Now i need to a method that simply does the following.
For each record in recordset in rs lookup firmID in rst1. If exists then print the line using debug.print. If it doesnt exist them go to next record in recordset
Jun 27 '13 #10
rizo98
18
@Adezil
Simple answer is no. An athelete will have also raced in another country too. So in this case (if
we were to doit in a query) i will need to link on athlete id and country id to get the correct last year position. So i will need to create more than one join while looping the array or recordset
Jun 27 '13 #11
ADezii
8,834 Expert 8TB
It appears as though you wish to test every Record in 2013 against every Record in 2012 based on a [FirmID] Field, thereby creating a Cartesian Product. If my assumption is correct, than this can be accomplished by Nested Recordsets. If my assumption is not correct, than I give up (LOL). In any event see if the below Logic will accomplish what you need.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rst As DAO.Recordset
  3. Dim rst1 As DAO.Recordset
  4.  
  5. Set MyDB = CurrentDb
  6. Set rst = MyDB.OpenRecordset("tbl2013", dbOpenForwardOnly)  '2013 Data
  7. Set rst1 = MyDB.OpenRecordset("tbl2012", dbOpenSnapshot)    '2012 Data
  8.  
  9. With rst
  10.   Do While Not .EOF                         'Loop thru ALL in tbl2013
  11.     Do While Not rst1.EOF                   'Loop thru ALL in tbl2012
  12.       If ![FirmID] = rst1![FirmID] Then     'Match foound on [FirmID]
  13.         Debug.Print ![FirmID]               'Print relvant 2013 Data
  14.         Debug.Print ![Perc]                 '           "
  15.         Debug.Print ![Turnover]             '           "
  16.         Debug.Print ![Total]                '           "
  17.         Debug.Print ![FirmName]             '           "
  18.  
  19.         Debug.Print rst1![FirmID]           'Print relvant 2012 Data
  20.         Debug.Print rst1![FirmName]         '           "
  21.         Debug.Print rst1![Perc]             '           "
  22.       End If
  23.         rst1.MoveNext                       'Next Record in 2012
  24.     Loop
  25.        rst1.MoveFirst                       'Move to First Record in 2012,
  26.                                             'Getting ready for next Iteration
  27.       .MoveNext                             'Next Record in 2013
  28.   Loop
  29. End With
  30.  
  31. 'Clean up after yourself
  32. rst.Close
  33. rst1.Close
  34. Set rst = Nothing
  35. Set rst1 = Nothing
Jun 28 '13 #12
Rabbit
12,516 Expert Mod 8TB
Why do you "have" to do it in VBA?
Jun 28 '13 #13
rizo98
18
@ADezii
You are correct. Your code is basically what I need.
However in your code only matching records are printed. Unmatching records are not printed with just their 2013 data.
what is the way around it?

I think the below need to to be placed somewhere, but where

Expand|Select|Wrap|Line Numbers
  1.  
  2.    If rst1.EOF Then
  3.  
  4.      Debug.Print ![FirmID], ![FirmName], ![Perc], ![Turnover], ![Total], rst1![FirmID], rst1![FirmName], rst1![Perc]
  5.      End If
  6.  
Jun 28 '13 #14
rizo98
18
@Rabbit
Needs to be done in VBA because there is more work / other work to be done in vba alongside
Jun 28 '13 #15
ADezii
8,834 Expert 8TB
If I am reading you correctly, we can make use of a Boolean Variable to let us know whether or not any Match(es) were found for each Record in the 2013 Data.
Expand|Select|Wrap|Line Numbers
  1. Dim blnMatchFound As Boolean
  2.  
  3. '********************** Code Intentionally omitted **********************
  4. With rst
  5.   Do While Not .EOF                         'Loop thru ALL in tbl2013
  6.     Do While Not rst1.EOF                   'Loop thru ALL in tbl2012
  7.       If ![FirmID] = rst1![FirmID] Then     'Match foound on [FirmID]
  8.         blnMatchFound = True                'At least 1 Match Found
  9.           Debug.Print ![FirmID]               'Print relvant 2013 Data
  10.           Debug.Print ![Perc]                 '           "
  11.           Debug.Print ![Turnover]             '           "
  12.           Debug.Print ![Total]                '           "
  13.           Debug.Print ![FirmName]             '           "
  14.  
  15.           Debug.Print rst1![FirmID]           'Print relvant 2012 Data
  16.           Debug.Print rst1![FirmName]         '           "
  17.           Debug.Print rst1![Perc]             '           "
  18.       End If
  19.         rst1.MoveNext                       'Next Record in 2012
  20.     Loop
  21.       'No Match(es) found for Iteration, print 2013 Data only
  22.       If Not blnMatchFound Then
  23.         Debug.Print ![FirmID], ![FirmName], ![Perc], ![Turnover], ![Total]
  24.       Else
  25.         'Match(es) found, must RESET for next Iteration of 2013 Data
  26.         blnMatchFound = False
  27.       End If
  28.        rst1.MoveFirst                       'Move to First Record in 2012,
  29.                                             'Getting ready for next Iteration
  30.       .MoveNext                             'Next Record in 2013
  31.   Loop
  32. End With
  33. '********************** Code Intentionally omitted **********************
  34.  
Jun 28 '13 #16
rizo98
18
Nice one. This should get me started. Is this the best approach for doing something like this? Using a recordset or an array? What other ways could it be done in thats quick and effecient?
Jul 1 '13 #17
rizo98
18
Also many thanks for the help/support guys. Well approciated
Jul 1 '13 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: J.D. Buehls | last post by:
I am displaying some values on a report. I am opening the .asp page with a Response.ContentType = "application/msword" and these particular values appear in a table. One column shows percentages...
4
by: Maur | last post by:
Hi all, I have 2 tables say t_OLD and t_NEW. The new has corrections for audit purposes. They are identical in all respects (i.e. new is a copy of old and then changes are made to t_new) ...
2
by: ano1optimist | last post by:
I have a form with a search button. I'm using command parameters to pass search criteria to a stored procedure. Here is my code: Stored procedure: CREATE PROCEDURE . @strCriteria varchar(200)...
0
by: Jason | last post by:
I have a primary form which is used to enter/edit data in a table named Test_Results. On this primary form there is a subform which displays site addresses. This subform is linked to the primary...
3
by: Brian Foree | last post by:
I am developing an ASP.NET application that uses Access 2000 as its backend, and have just started getting the following error on 2 ASP.NET pages that had been working until late last week (and I...
3
by: GeryLuz | last post by:
Hi! I can't find how to build an SQL statement inside an ASP page using another open recordset as its where clause. My code is the following: Set rs0 = cn.Execute ("Select Id_Fam from...
10
kamill
by: kamill | last post by:
Dear All, I need to send some data from my site to another site, but i do not want to hit that site. Is there any option. Currently i am using form to doing it but after submitting it, i do not...
2
dnb
by: dnb | last post by:
how can we fill data in same mshflexgrid using two recordset? Some from one & some from another recordset? please give reply
1
by: Chandan Kr Sah | last post by:
How to use like command in code behind page to compare with recordset value ? As I want to do comparision like below Dim rs As DAO.Recordset If rs1!.Value Not Like & '*Prep*'" Then '...do...
1
by: lipton | last post by:
how can i compare a mysql table feild value with selected redio button value with php
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.