Delete By Date

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to write some code that will let me delete records that are 180
days old from the date that the code is activated. The problem with this is
that I have many tables in my database that are unrelated with each table
having a different set of records. All of the tables however have a date in
it.
Is my only solution is to make a bunch of delete querries for all of the
tables and call them by code or is there a better way????

Thank You For Any Help
Mallory
 
Hi, Mallory.
Is my only solution is to make a bunch of delete querries for all of the
tables and call them by code or is there a better way????

If you're like me, thatsa lotsa delete queries. I'd automate it with VBA
code, instead. First, I'd create a table with the following structure:

Table Name: tblDateFields
ID, AutoNumber, primary key
TblName, Text
DateField, Text

Then I'd run the following code to store the table names and date fields in
this new table:

'=============================================
' This sub iterates through all of the fields in each non-system table and
' stores the name of the table and the name of the date field in the
' tblDateFields table.
'=============================================

Public Sub findDateFields()

On Error GoTo ErrHandler

Dim db As Database
Dim tbl As TableDef
Dim idx As Long

Set db = CurrentDb()

For Each tbl In db.TableDefs
If (Left$(tbl.Name, 4) <> "MSys") Then
For idx = 0 To (tbl.Fields.Count - 1)
If (tbl.Fields(idx).Type = dbDate) Then
CurrentDb.Execute "INSERT INTO tblDateFields (TblName,
DateField) " & _
"VALUES ('" & tbl.Name & "', '" &
tbl.Fields(idx).Name & "');", dbFailOnError
End If
Next idx
End If
Next tbl

CleanUp:

Set tbl = Nothing
Set db = Nothing

Exit Sub

ErrHandler:

MsgBox "Error in findDateFields( )." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear
GoTo CleanUp

End Sub

Then I'd open the table and ensure that only one date field was saved for
each table. (If there were more than one, I'd have to decide which date
field to keep as the determiner for the "age" of the record, then delete the
others.) Then I'd run the following code whenever I needed to get rid of the
"expired" records:

'=============================================
' This sub iterates through all of the tables in the database (except for the
' non-system tables) and deletes all records more than 180 days old. It
' compares the value in the date field of each table (named date field for
each
' table is stored in the tblDateFields table), then deletes all of the
records more
' than 180 days old. This sub assumes that there is only one date field per
' table. DAO Object Library reference required.
'=============================================

Public Sub deleteExpiredRecs()

On Error GoTo ErrHandler

Dim db As Database
Dim tbl As TableDef
Dim recSet As DAO.Recordset
Dim fldTable As DAO.Field
Dim fldDate As DAO.Field
Dim idx As Long
Dim fOpenedRecSet As Boolean
Dim fDone As Boolean

Set db = CurrentDb()
Set recSet = CurrentDb().OpenRecordset("tblDateFields")
Set fldTable = recSet.Fields("TblName")
Set fldDate = recSet.Fields("DateField")
recSet.MoveLast

For Each tbl In db.TableDefs
recSet.MoveFirst
fDone = False

Do Until (fDone Or recSet.EOF)
If (Left$(tbl.Name, 4) <> "MSys") Then
If (tbl.Name = fldTable.Value) Then
CurrentDb().Execute "DELETE * " & _
"FROM " & tbl.Name & _
" WHERE (" & fldDate.Value & " < Date( ) - 180)",
dbFailOnError
fDone = True
End If

If (Not (recSet.EOF)) Then
recSet.MoveNext
End If
Else
fDone = True
End If
Loop
Next tbl

CleanUp:

Set fldDate = Nothing
Set fldTable = Nothing
Set tbl = Nothing
Set db = Nothing

If (fOpenedRecSet) Then
recSet.Close
fOpenedRecSet = False
End If

Exit Sub

ErrHandler:

MsgBox "Error in deleteExpiredRecs( ) in TestModule." & vbCrLf & vbCrLf
& _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear
GoTo CleanUp

End Sub

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
You might consider creating a table to hold the names of the tables to
be purged along with the selection criteria. From there it you could use
DAO to grab the information and create the DELETE queries on the fly.
You'll probably want to do something to ensure that the table isn't
modifyied by any users. Maybe putting it in a separate FE Admin DB.

tblPurgeTableMaster
txtTableName tblReservations
txtSelectionCriteria dteDate > Now() AND txtStatus = 'XCL'
dteLastPurge 10/31/2005 7:05:34 AM

To make things even nicer you could wrap all of the DELETE queries in a
transaction to ensure that they all succeed or fail.

David H
 
It was a genuine idea, REALLY it was! lol

'69 Camaro said:
Hi, Mallory.




If you're like me, thatsa lotsa delete queries. I'd automate it with VBA
code, instead. First, I'd create a table with the following structure:

Table Name: tblDateFields
ID, AutoNumber, primary key
TblName, Text
DateField, Text

Then I'd run the following code to store the table names and date fields in
this new table:

'=============================================
' This sub iterates through all of the fields in each non-system table and
' stores the name of the table and the name of the date field in the
' tblDateFields table.
'=============================================

Public Sub findDateFields()

On Error GoTo ErrHandler

Dim db As Database
Dim tbl As TableDef
Dim idx As Long

Set db = CurrentDb()

For Each tbl In db.TableDefs
If (Left$(tbl.Name, 4) <> "MSys") Then
For idx = 0 To (tbl.Fields.Count - 1)
If (tbl.Fields(idx).Type = dbDate) Then
CurrentDb.Execute "INSERT INTO tblDateFields (TblName,
DateField) " & _
"VALUES ('" & tbl.Name & "', '" &
tbl.Fields(idx).Name & "');", dbFailOnError
End If
Next idx
End If
Next tbl

CleanUp:

Set tbl = Nothing
Set db = Nothing

Exit Sub

ErrHandler:

MsgBox "Error in findDateFields( )." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear
GoTo CleanUp

End Sub

Then I'd open the table and ensure that only one date field was saved for
each table. (If there were more than one, I'd have to decide which date
field to keep as the determiner for the "age" of the record, then delete the
others.) Then I'd run the following code whenever I needed to get rid of the
"expired" records:

'=============================================
' This sub iterates through all of the tables in the database (except for the
' non-system tables) and deletes all records more than 180 days old. It
' compares the value in the date field of each table (named date field for
each
' table is stored in the tblDateFields table), then deletes all of the
records more
' than 180 days old. This sub assumes that there is only one date field per
' table. DAO Object Library reference required.
'=============================================

Public Sub deleteExpiredRecs()

On Error GoTo ErrHandler

Dim db As Database
Dim tbl As TableDef
Dim recSet As DAO.Recordset
Dim fldTable As DAO.Field
Dim fldDate As DAO.Field
Dim idx As Long
Dim fOpenedRecSet As Boolean
Dim fDone As Boolean

Set db = CurrentDb()
Set recSet = CurrentDb().OpenRecordset("tblDateFields")
Set fldTable = recSet.Fields("TblName")
Set fldDate = recSet.Fields("DateField")
recSet.MoveLast

For Each tbl In db.TableDefs
recSet.MoveFirst
fDone = False

Do Until (fDone Or recSet.EOF)
If (Left$(tbl.Name, 4) <> "MSys") Then
If (tbl.Name = fldTable.Value) Then
CurrentDb().Execute "DELETE * " & _
"FROM " & tbl.Name & _
" WHERE (" & fldDate.Value & " < Date( ) - 180)",
dbFailOnError
fDone = True
End If

If (Not (recSet.EOF)) Then
recSet.MoveNext
End If
Else
fDone = True
End If
Loop
Next tbl

CleanUp:

Set fldDate = Nothing
Set fldTable = Nothing
Set tbl = Nothing
Set db = Nothing

If (fOpenedRecSet) Then
recSet.Close
fOpenedRecSet = False
End If

Exit Sub

ErrHandler:

MsgBox "Error in deleteExpiredRecs( ) in TestModule." & vbCrLf & vbCrLf
& _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear
GoTo CleanUp

End Sub

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.


:
 
Back
Top