Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Find Duplicates    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Duplicate Records
Greg Rosoff 
     
2 years ago
I have a funny situation.  I get a staff roster that has five elements that should create unique records.  The source database must have additional attributes that are not made available to me, as I get duplicate records (generally two duplicates per record, perhaps 9,000 out of 300,000 every month).  I want to create a combination of a query that identifies the duplicates, and then a vba script that updates a "Duplicate_EDIPI" field so that I can retain the records (to take back to the source) but strip them out from other functions/queries.  

SQL Statement:

SELECT TOP 2 *
FROM (SELECT EDIPI, TI_CD, DMHRSI_Emp_Number, Count(*) AS [Count]
FROM tbl_DMHRSI_240930
GROUP BY EDIPI, TI_CD, DMHRSI_Emp_Number
HAVING COUNT(*) >1
)  AS Duplicates
ORDER BY Duplicates.EDIPI DESC;

VBA:

DetailsPrivate Sub Identify_Duplicate_EDIPIs_Click()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim i As Integer
    
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("qry_Duplicate_EDIPI_Amended")
    
    i = 0
    While Not rs.EOF
        i = i + 1
        If i = 2 Then 'update the second duplicate record
        'Update code here
            rs!Duplicate_EDIPI = 1
            rs.Update
        End If
        rs.MoveNext
    Wend
    
    rs.Close
    Set rs = Nothing
    Set db = Nothing
End Sub


This isn't working out and was hoping I could be put on the right path by this community!

Kevin Robertson  @Reply  
          
2 years ago
You have a period separating your field names in your ORDER BY clause. It should be a comma.
Count(*) AS [Count] - Count is a reserved word. Don't use it as an alias.

When you say "this isn't working out", what is happening when the code runs?
Are you getting any error messages? If so, what are they?
Greg Rosoff OP  @Reply  
     
2 years ago
Thank you for replying!

The query as I plagiarized and then customized is selecting the top two ID numbers (EDIPI), whereas I need the query to take and then separate the two same ID numbers (EDIPI).

For Instance:
ID
1112519
9306546
6548916
1112945
6549898
1112945
6549887
1112519

I need the query to select and 'Count'
ID              Count
1112519             1
1112519             2

And then loop through the whole list.

Right now, it is selecting:
ID              Count
1112519             2
1112945             2
Kevin Yip  @Reply  
     
2 years ago
hi Greg, your SQL statement creates an aggregated query.  Aggregated queries are non-updateable.  To make an updateable query, the query needs to be non-aggregate, and it needs to contain the table you need to update.  Save your SQL statement as, say, Query1.  Then create another query that joins Query1 with the table that has the field(s) you want to update:

     SELECT Table1.Field1, ... INNER JOIN Query1 ON Table1.Field1 = Query1.Field1

Use this SQL for OpenRecordset in VBA.  The resulting recordset will allow updates to Table1.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Find Duplicates.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 4/13/2026 6:35:20 AM. PLT: 0s