简体   繁体   中英

Filtering Data using record sets

Hello everyone I am at a dilemma I am trying to find possible multiple accounts made by the same person by comparing the records, my main problem is how do you compare the current row value to the next value? By this I mean like this MI(i)=MI(i+1) "MI" is the field that I am targeting and i represents the current row, so what this is suppose to do is compare the current value to the next value. But it does not seem to work in vba is it done a diffferent way? To filter should I use rs.Filter or should I use Me.Filter because I want the filtered table to show up in my splitform.

Current Example Table that I am working with:

 Number       MI      First Name     Last Name      DOB 
15241543  123456789    James          Matheson    2/25/1980 
15241543  123456789    Jams           Matheson    2/25/1980 
12345679  124512451    Monroe         Matheson    3/25/1980 
12345679  124512451    Monro          Matheson    3/25/1980 
54789654  152415241    Dilan          Pumley      4/23/1970 
54789658  154215246    Michael        Lan         1/30/1989 

Final table that should appear is this:

 Number       MI      First Name     Last Name      DOB 
15241543  123456789    James          Matheson    2/25/1980 
15241543  123456789    Jams           Matheson    2/25/1980 
12345679  124512451    Monroe         Matheson    3/25/1980 
12345679  124512451    Monro          Matheson    3/25/1980 


Private Sub buttonSort_Click()

Dim i As Integer
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("deleteYo")

For i = 0 To rs.RecordCount - 1
'Comapred the current DOB cell value to the next DOB cell value and some other conditions are set such as Current First Name does not equal the next First Name value
If DOB(i) = DOB(i + 1) And [First Name](i) <> [First Name](i + 1) Or [Last Name](i) <>  [Last Name](i + 1) And Number(i) = Number(i + 1) Or MI(i) = MI(i + 1) Then
'Filters the current value if the conditon is passed
Me.Filter = "[First Name]" & [First Name](i).Value & "'"
'Filter the next value also to append the filtered values so that they will not be overwritten.
Me.Filter = Me.Filter & "[First Name]" & [First Name](i + 1).Value & "'"
Else

End If
rs.MoveNext
Next i
Me.FilterOn = True
rs.Close
Set rs = Nothing
db.Close

End Sub

Please Note I have tried using a query with SQl statement for this which does not work properly perhaps because this data is practically millions of records and also because the UI is in the form and when the filtered values are applied I would like them to show up at the table at the bottom of the split form.

You can try using a corelated subquery.

SELECT t.ID, t.[First Name], t.[LastName], t.MI
FROM YourTable AS t

   LEFT JOIN

          (SELECT t2.id, t2.[First Name], t2.[Last Name], t2.MI
            FROM YourTable AS t2) AS temp

   ON t.MI = temp.MI
   WHERE t.[First Name] != t2.[First Name]
       OR
         t.[Last Name] != t2.[Last Name];

This is going to compare each field with the same MI value and retrieve records where the first and last names are not similar.

You can adjust the fields to better fit your table schema.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM