Solved

Delete based on Column, and date added

Posted on 2011-09-17
11
243 Views
Last Modified: 2012-05-12
The attached module works quickly, but, what I need is to put in one more criteria, where it deletes the oldest, and leaves the newest.

For example it would keep the one with the 9/30/2011 date

Contraint Number              Date

453                                   9/01/2011
453                                   9/30/2011
Option Compare Database

Function DeleteDuplicates()
    Dim DeleteCount As Double ' Make this a Long if you really have that many records
    Dim temp
    Dim sql As String
    Dim rst As Recordset

    
    '' Change DESC to ASC to keep the older record
    '' Leave DESC to keep the newer record
    sql = "SELECT [tbl_GMNA Constraint Report Output].* FROM [tbl_GMNA Constraint Report Output] ORDER BY [tbl_GMNA Constraint Report Output].[Constraint Number] DESC"

    Set rst = CurrentDb.OpenRecordset(sql, dbOpenDynaset)
    
    If rst.EOF Then
        MsgBox "There are no records in this table!"
        Exit Function
    End If
    'Stop
    rst.MoveFirst
    Do Until rst.EOF
        If temp = rst![Constraint Number] Then
            rst.Delete
            DeleteCount = DeleteCount + 1
        Else
            temp = rst![Constraint Number]
        End If
        rst.MoveNext
    Loop
    Set rst = Nothing
    MsgBox "Found and deleted " & CStr(DeleteCount) & " records."

 End Function

Open in new window

0
Comment
Question by:mato01
  • 7
  • 4
11 Comments
 
LVL 16

Expert Comment

by:Sheils
ID: 36555449
Try this

 
Option Compare Database

Function DeleteDuplicates()
    Dim DeleteCount As Double ' Make this a Long if you really have that many records
    Dim temp
    Dim sql As String
	Dim sql2 as String
    Dim rst As Recordset
	Dim rst2 As Recordset
	Dim datMax as Date

    
    '' Change DESC to ASC to keep the older record
    '' Leave DESC to keep the newer record
    sql = "SELECT [tbl_GMNA Constraint Report Output].* FROM [tbl_GMNA Constraint Report Output] ORDER BY [tbl_GMNA Constraint Report Output].[Constraint Number] DESC"

    Set rst = CurrentDb.OpenRecordset(sql, dbOpenDynaset)
    
    If rst.EOF Then
        MsgBox "There are no records in this table!"
        Exit Function
    End If
    'Stop
    rst.MoveFirst
    Do Until rst.EOF
        If temp = rst![Constraint Number] Then
            
			sql2="SELECT [tbl_GMNA Constraint Report Output].* FROM [tbl_GMNA Constraint Report Output]" 
			sql2=sql2 & " WHERE [Constraint Number]=" & rst("Constraint Number") 
			sql2=sql2 & " ORDER BY [tbl_GMNA Constraint Report Output].[Constraint Number] DESC"
			
			rst2.open sql2
			
			datMax=rst2("fldDate")
			
			if rst2.Count>1 Then
			
			sql2="Delete * From [tbl_GMNA Constraint Report Output] WHERE [Constraint Number]=" & rst("Constraint Number")
			sql2=sql2 & " AND fldDate Is Not #" & datMax & "#"
			
			CurrentDB.Execute sql2
			
			End If
			
			rst2.close
			
            DeleteCount = DeleteCount + 1
        Else
            temp = rst![Constraint Number]
        End If
        rst.MoveNext
    Loop
    Set rst = Nothing
    MsgBox "Found and deleted " & CStr(DeleteCount) & " records."

 End Function

Open in new window

0
 

Author Comment

by:mato01
ID: 36555488
It gave an error "Compile Error: Method or data member not found"

at below line

rst2.Open sql2
0
 
LVL 16

Expert Comment

by:Sheils
ID: 36555542
change fldDate to the name of the field that holds the date in your db and also change rst2.Count to rst2.RecordCount
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

Author Comment

by:mato01
ID: 36556337
Make above suggested changes, and still "Compile Error: Method or data member not found" at
rst2.Open sql2. Did not work.  Also, tried to change rst2.Open sql2 to rst2.OpenRecordset sql2.
***********************************************************************************************************************
            rst2.Open sql2

            datMax = rst2("Date Added")
           
            If rst2.RecordCount > 1 Then
0
 
LVL 16

Expert Comment

by:Sheils
ID: 36556962
try:

sql2="SELECT [tbl_GMNA Constraint Report Output].* FROM [tbl_GMNA Constraint Report Output]"
                  sql2=sql2 & " WHERE [Constraint Number]=" & rst.fields("Constraint Number")
                  sql2=sql2 & " ORDER BY [tbl_GMNA Constraint Report Output].[Constraint Number] DESC"
                  
                  rst2.open sql2
                  
                  datMax=rst2.fields("fldDate")

My apology for the syntax errors.

If you still have problem do a debug.print and post the output. That is

sql2="SELECT [tbl_GMNA Constraint Report Output].* FROM [tbl_GMNA Constraint Report Output]"
sql2=sql2 & " WHERE [Constraint Number]=" & rst.fields("Constraint Number")
sql2=sql2 & " ORDER BY [tbl_GMNA Constraint Report Output].[Constraint Number] DESC"

Debug.print sql2

The output will be in the immediate window. Copy and post                  
                  
0
 

Author Comment

by:mato01
ID: 36557541
That didn't work.  This is new for me.  How do I debug.print?
0
 

Author Comment

by:mato01
ID: 36557918
I've attached a sample of the database for reference.

The error message is

COMPILE ERROR: Method or Data Member Not Found highlights this line.

  rst2.Open sql2

test11.accdb
0
 
LVL 16

Accepted Solution

by:
Sheils earned 500 total points
ID: 36559079
This works

 
Function DeleteDuplicates()
    Dim DeleteCount As Long ' Make this a Long if you really have that many records
    Dim temp
    Dim sql As String
    Dim sql2 As String
    Dim rst As DAO.Recordset
    Dim rst2 As DAO.Recordset
    Dim datMax As Date

    
    '' Change DESC to ASC to keep the older record
    '' Leave DESC to keep the newer record
    sql = "SELECT [tbl_GMNA Constraint Report Output].* FROM [tbl_GMNA Constraint Report Output] ORDER BY [tbl_GMNA Constraint Report Output].[Constraint Number] DESC"

    Set rst = CurrentDb.OpenRecordset(sql, dbOpenDynaset)

    If rst.EOF Then
        MsgBox "There are no records in this table!"
        Exit Function
    End If
        
    rst.MoveFirst
    Do Until rst.EOF
  
        If temp = rst![Constraint Number] Then
        
        
        sql2 = "SELECT [tbl_GMNA Constraint Report Output].* FROM [tbl_GMNA Constraint Report Output]"
                  sql2 = sql2 & " WHERE [Constraint Number]='" & rst.Fields("Constraint Number")
                  sql2 = sql2 & "' ORDER BY [tbl_GMNA Constraint Report Output].[Constraint Number] DESC"
                  
                  
                  Set rst2 = CurrentDb.OpenRecordset(sql2, dbOpenDynaset)
                  
                  datMax = rst2.Fields("Date Added")

            
            If rst2.RecordCount > 1 Then
            
            sql2 = "Delete * From [tbl_GMNA Constraint Report Output] WHERE [Constraint Number]=" & rst("Constraint Number")
            sql2 = sql2 & " AND [Date Added] Is Not #" & datMax & "#"
            
            CurrentDb.Execute sql2
            
            End If
            
            rst2.Close
            
            DeleteCount = DeleteCount + 1
        Else
            temp = rst![Constraint Number]
        End If
        rst.MoveNext
    Loop
    Set rst = Nothing
    MsgBox "Found and deleted " & CStr(DeleteCount) & " records."

End Function

Open in new window

0
 

Author Comment

by:mato01
ID: 36567469
When I ran it, I received the following error.

Run-time error '3075.:

Invalid use of IS operator in query expression '[Constraint Number] = 766 AND [Date Added] Is not #9/11/2011'.
0
 

Author Comment

by:mato01
ID: 36567528
Changed

sql2 = sql2 & " AND [Date Added] Is Not #" & datMax & "#"

to

sql2 = sql2 & " AND [Date Added] <> #" & datMax & "#"

and received a  Run-time error '3464'

Data type mismatch in criteria expression
0
 

Author Closing Comment

by:mato01
ID: 36568899
I was able to get an answer.  But, wanted to award you ALL the points for all your great help.  Thanks
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Normalization of a table 19 75
Access 2003 query lost it's only join 7 27
Help with DoEvents 8 28
Access summarise unique values in tables 2 16
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question