Hi all,
I have an access database (97) and I have a main table (Rectrans) and several child tables (ADV, CBL etc)
Basically, I want to be able to delete records that are more than 7 years old from the last date in the Rectrans table, and any related records in the child tables.
Here is the code:
------
Private Sub mybutton12355_Click()
Set Db = CurrentDb()
Dim strSQL As String
'
strSQL = "DELETE FROM RecTrans WHERE " & _
"ContractNumber IN SELECT " & _
"RECTRANS.ContractNumber " & _
"FROM RECTRANS " & _
"GROUP BY RECTRANS.ContractNumber " & _
"HAVING (((DateAdd('yyyy',7,Max([RECTRANS].[TransactionDate])))<Now())));"
'
strSQL = "DELETE FROM ADV WHERE ContractNumber NOT IN (Select ContractNumber " & _
"FROM Rectrans);"
DoCmd.RunSQL strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
strSQL = "DELETE FROM CBL WHERE ContractNumber NOT IN (Select ContractNumber " & _
"FROM Rectrans);"
DoCmd.RunSQL strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
strSQL = "DELETE FROM LEASE WHERE ContractNumber NOT IN " & _
"(Select ContractNumber FROM Rectrans);"
DoCmd.RunSQL strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
strSQL = "DELETE FROM PL WHERE ContractNumber NOT IN " & _
"(Select ContractNumber FROM Rectrans);"
DoCmd.RunSQL strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
strSQL = "DELETE FROM HP WHERE ContractNumber NOT IN " & _
"(Select ContractNumber " & _
"FROM Rectrans);"
DoCmd.RunSQL strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
Set Db = Nothing
End Sub
------
Bascially, when I put my clock forward 7 years, it should delete several records (I have checked this via a select query) but it doesn't, it just says "you are about to delete 0 records"
Can someone please verify my code?
Thanks
I have an access database (97) and I have a main table (Rectrans) and several child tables (ADV, CBL etc)
Basically, I want to be able to delete records that are more than 7 years old from the last date in the Rectrans table, and any related records in the child tables.
Here is the code:
------
Private Sub mybutton12355_Click()
Set Db = CurrentDb()
Dim strSQL As String
'
strSQL = "DELETE FROM RecTrans WHERE " & _
"ContractNumber IN SELECT " & _
"RECTRANS.ContractNumber " & _
"FROM RECTRANS " & _
"GROUP BY RECTRANS.ContractNumber " & _
"HAVING (((DateAdd('yyyy',7,Max([RECTRANS].[TransactionDate])))<Now())));"
'
strSQL = "DELETE FROM ADV WHERE ContractNumber NOT IN (Select ContractNumber " & _
"FROM Rectrans);"
DoCmd.RunSQL strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
strSQL = "DELETE FROM CBL WHERE ContractNumber NOT IN (Select ContractNumber " & _
"FROM Rectrans);"
DoCmd.RunSQL strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
strSQL = "DELETE FROM LEASE WHERE ContractNumber NOT IN " & _
"(Select ContractNumber FROM Rectrans);"
DoCmd.RunSQL strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
strSQL = "DELETE FROM PL WHERE ContractNumber NOT IN " & _
"(Select ContractNumber FROM Rectrans);"
DoCmd.RunSQL strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
strSQL = "DELETE FROM HP WHERE ContractNumber NOT IN " & _
"(Select ContractNumber " & _
"FROM Rectrans);"
DoCmd.RunSQL strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
Set Db = Nothing
End Sub
------
Bascially, when I put my clock forward 7 years, it should delete several records (I have checked this via a select query) but it doesn't, it just says "you are about to delete 0 records"
Can someone please verify my code?
Thanks
