Hi there,
I wonder if anyone could help me out with this problem:
I am trying to compare two recordsets with different numbers of records to see if there are any closely matched records in each. I need to know for each record in the first recordset whether there is a close match in the second recordset.
For the sake of simplicty I am only comparing the values in one field initially, although I may need to expand the routine to include two or perhaps three fields in the future.
The code I am working with so far is as follows:
Sub MatchForenames()
Dim strSQL1 As String
Dim strSQL2 As String
Dim num2000 As Integer, num2002 As Integer
Dim counter As Integer, counter2 As Integer
Dim wksCorin As DAO.Workspace
Dim dbsGPlus As DAO.Database
Dim rstForename2000 As DAO.Recordset
Dim rstForename2002 As DAO.Recordset
Dim fld As DAO.Field
Set wksCorin = DBEngine.Workspaces(0)
Set dbsGPlus = wksCorin.Databases(0)
strSQL1 = "SELECT [2000 GCSE Numbers].Forename FROM [2000 GCSE Numbers] ORDER BY [2000 GCSE Numbers].Forename"
Set rstForename2000 = dbsGPlus.OpenRecordset(strSQL1, dbOpenDynaset)
rstForename2000.MoveLast
'Debug.Print "2000 GCSE Numbers Record Count is : " & rstForename2000.RecordCount
strSQL2 = "SELECT [2002 Numbers].Forename FROM [2002 Numbers] ORDER BY [2002 Numbers].Forename"
Set rstForename2002 = dbsGPlus.OpenRecordset(strSQL2, dbOpenDynaset)
rstForename2002.MoveLast
'Debug.Print "2002 Numbers Record Count is : " & rstForename2002.RecordCount
num2000 = rstForename2000.RecordCount
num2002 = rstForename2002.RecordCount
rstForename2000.MoveFirst
rstForename2002.MoveFirst
'For each field in rst1 loop through every field in rst2 - print matches
For counter = 0 To num2000
For counter2 = 0 To num2002
For Each fld In rstForename2002
If fld.Value = rstForename2000.Fields(counter).Value Then
Debug.Print rstForename2000.Fields(counter).Value
End If
rstForename2002.MoveNext
Next fld
rstForename2000.MoveNext
Next counter
Set rstForename2000 = Nothing
Set rstForename2002 = Nothing
Set wksCorin = Nothing
Set dbsGPlus = Nothing
End Sub
Unfortunately, this doesn't seem to work, and I'm not sure why that is.
At present I am looking only for exact matches, but ultimately I want o be able to compare using a "*" or similar operator to return close matches. The forename field in the first rst has multiple forenames, and I will eventually need to match these to the single forenames in the second rst - then I can adjust the two so that they are reconciled.
I've tried searching this site but haven't yet found a clear enough explanation of how to compare two different recordsets, so if anyone can enlighten me I'd be most grateful.
Thanks in advance!
CJA
I wonder if anyone could help me out with this problem:
I am trying to compare two recordsets with different numbers of records to see if there are any closely matched records in each. I need to know for each record in the first recordset whether there is a close match in the second recordset.
For the sake of simplicty I am only comparing the values in one field initially, although I may need to expand the routine to include two or perhaps three fields in the future.
The code I am working with so far is as follows:
Sub MatchForenames()
Dim strSQL1 As String
Dim strSQL2 As String
Dim num2000 As Integer, num2002 As Integer
Dim counter As Integer, counter2 As Integer
Dim wksCorin As DAO.Workspace
Dim dbsGPlus As DAO.Database
Dim rstForename2000 As DAO.Recordset
Dim rstForename2002 As DAO.Recordset
Dim fld As DAO.Field
Set wksCorin = DBEngine.Workspaces(0)
Set dbsGPlus = wksCorin.Databases(0)
strSQL1 = "SELECT [2000 GCSE Numbers].Forename FROM [2000 GCSE Numbers] ORDER BY [2000 GCSE Numbers].Forename"
Set rstForename2000 = dbsGPlus.OpenRecordset(strSQL1, dbOpenDynaset)
rstForename2000.MoveLast
'Debug.Print "2000 GCSE Numbers Record Count is : " & rstForename2000.RecordCount
strSQL2 = "SELECT [2002 Numbers].Forename FROM [2002 Numbers] ORDER BY [2002 Numbers].Forename"
Set rstForename2002 = dbsGPlus.OpenRecordset(strSQL2, dbOpenDynaset)
rstForename2002.MoveLast
'Debug.Print "2002 Numbers Record Count is : " & rstForename2002.RecordCount
num2000 = rstForename2000.RecordCount
num2002 = rstForename2002.RecordCount
rstForename2000.MoveFirst
rstForename2002.MoveFirst
'For each field in rst1 loop through every field in rst2 - print matches
For counter = 0 To num2000
For counter2 = 0 To num2002
For Each fld In rstForename2002
If fld.Value = rstForename2000.Fields(counter).Value Then
Debug.Print rstForename2000.Fields(counter).Value
End If
rstForename2002.MoveNext
Next fld
rstForename2000.MoveNext
Next counter
Set rstForename2000 = Nothing
Set rstForename2002 = Nothing
Set wksCorin = Nothing
Set dbsGPlus = Nothing
End Sub
Unfortunately, this doesn't seem to work, and I'm not sure why that is.
At present I am looking only for exact matches, but ultimately I want o be able to compare using a "*" or similar operator to return close matches. The forename field in the first rst has multiple forenames, and I will eventually need to match these to the single forenames in the second rst - then I can adjust the two so that they are reconciled.
I've tried searching this site but haven't yet found a clear enough explanation of how to compare two different recordsets, so if anyone can enlighten me I'd be most grateful.
Thanks in advance!
CJA