Alex L
Alex L

Reputation: 5

Delete a record from a database after a certain amount of time has passed

I'm asking for help again for another school project. So, the plan is to insert a record into a "Deleted Records" database and delete that from that database after a specific number of time has passed. For example, I inserted a record into that database and I want it deleted from the same database "exactly three years later". The current template in my head is:

Insert the record into the database along with the date it was inserted (Date.Today.ToShortDateString).
If row.Cells(8).Value (the date it was inserted) reached exactly 3 years old Then
Delete it from the database.
End If

I know how to insert and delete into and from a database. I just don't know what's the code when the date it was inserted reaches 3 years old.

Upvotes: 0

Views: 430

Answers (2)

Kostas K.
Kostas K.

Reputation: 8518

I just don't know what's the code when the date it was inserted reaches 3 years old.

Using the DateAdd function, add 3 years to the date the record was inserted and compare it to today's date.

DateAdd("yyyy", 3, [DateInsertedFieldName]) >= Date()

You can put the above condition on the WHERE clause of your delete query.

Upvotes: 2

Jiachen Li-MSFT
Jiachen Li-MSFT

Reputation: 483

You can add a column of Date in Deleted Records, and use Current Date to automatically record the date when each piece of data was added.

Write a method for your program to delete data older than three years, and call it automatically under certain circumstances.

Upvotes: 0

Related Questions