Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Help   Contact   Merch   Join   Order   Logon   Forums   
 
Back to Access Developers    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Removing Duplicates
Bob George 
   
75 minutes ago
Hi I have done something simple wrong but just can't see it. Can someone help please.

Private Sub Command6_Click()
    
    Sub RemoveDuplicatesRecordset()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim strSQL As String
    Dim varLastValue As Variant
    
    Set db = CurrentDb()
    
    ' SQL query must SORT the table by the duplicate field to group them together
    strSQL = "SELECT Other_Partyb FROM CsvInter_T ORDER BY Other_Partyb;"
    Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
    
    ' Initialize value check variable
    varLastValue = Null
    
    ' Loop through the rows
    If Not (rs.BOF And rs.EOF) Then
        rs.MoveFirst
        Do Until rs.EOF
            ' Check if current row matches the last saved value
            If rs!Other_Partyb = varLastValue Then
                rs.Delete
            Else
                ' Update the variable to hold the new unique value
                varLastValue = rs!Other_Partyb
            End If
            rs.MoveNext
        Loop
    End If
    
    ' Clean up variables
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    
    MsgBox "Duplicate loop cleanup complete!", vbInformation, "Done"
End Sub
End Sub
Kevin Robertson  @Reply  
          
71 minutes ago
You have an extra End Sub.
Kevin Robertson  @Reply  
          
69 minutes ago
Sub RemoveDuplicatesRecordset() is inside the button's click event.
Richard Rost  @Reply  
          
68 minutes ago
Kevin is right. The bigger issue is that you're trying to declare one Sub inside another Sub. Your outer procedure is Command6_Click, and RemoveDuplicatesRecordset needs to be a separate procedure at the form's module level.

End the button click event first, then put the duplicate-removal routine below it, like this in structure:

Private Sub Command6_Click()

    RemoveDuplicatesRecordset

End Sub

Private Sub RemoveDuplicatesRecordset()

    ' Your code goes here

End Sub

You only need one End Sub for each procedure. Also, calling RemoveDuplicatesRecordset from the button click event is what actually runs the code when you click the button.

Add a Reply Upload an Image
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: 7/31/2026 6:30:33 PM. PLT: 1s