Hello Everybody.
I was wondering if anyone could please help me with a SQL syntax issue that I am currently experiencing problems with.
I have a requirement to archive data over 2 years old, the data from which extracted from two tables on which I there is a one-to-many relationship. The PK from Table A is a National Insurance No which acts as the FK in Table B.
I managed to source some code (included below), which does a great job for one table (Table A), but I am unsure as to how I need to modify it in order to collect and archive its associated data in Table B. I have searched various forums for a solution but to no avail….
The fields in Table B are strNINo, strTrgProvider, strTrgActivity, strSector, strTutor and TrgCost
……………………………………………………………………………………………………..
What I Currently Have Is This:
Private Sub cmdArchiveData_Click()
'Run Archive - Append and Delete
Dim strSQLAppend As String
Dim strSQLDelete As String
Dim errLoop As Error
Dim dteExpiry As Date
dteExpiry = DateAdd("yyyy", -2, Date)
' Define two SQL statements for action queries.
strSQLAppend = "INSERT INTO tblExpiredStudents " & _
"( strNINo, strFirstName, strLastName, strAddress1, " & _
"strAddress2, strCity, strCounty, strPostCode, strTelephone, " & _
"hypE-mailAddress, dtmDOB, dtmEnrolled, strCourseID ) " & _
"SELECT tblStudentInformation.strNINo, " & _
"tblStudentInformation.strFirstName, " & _
"tblStudentInformation.strLastName, " & _
"tblStudentInformation.strAddress1, " & _
"tblStudentInformation.strAddress2, " & _
"tblStudentInformation.strCity, " & _
"tblStudentInformation.strCounty, " & _
"tblStudentInformation.strPostCode, " & _
"tblStudentInformation.strTelephone, " & _
"tblStudentInformation.E-mailAddress, " & _
"tblStudentInformation.dtmDOB, " & _
"tblStudentInformation.dtmEnrolled, " & _
"tblStudentInformation.strCourseID " & _
"FROM tblStudentInformation " & _
"WHERE tblStudentInformation.dtmEnrolled <= #" & dteExpiry & "#;"
strSQLDelete = "DELETE tblStudentInformation.strNINo, " & _
"tblStudentInformation.strFirstName, " & _
"tblStudentInformation.strLastName, " & _
"tblStudentInformation.strAddress1, " & _
"tblStudentInformation.strAddress2, " & _
"tblStudentInformation.strCity, " & _
"tblStudentInformation.strCounty, " & _
"tblStudentInformation.strPostCode, " & _
"tblStudentInformation.strTelephone, " & _
"tblStudentInformation.hypE-mailAddress, " & _
"tblStudentInformation.dtmDOB, " & _
"tblStudentInformation.dtmEnrolled, " & _
"tblStudentInformation.strCourseID " & _
"FROM tblStudentInformation " & _
"WHERE tblStudentInformation.dtmEnrolled <= #" & dteExpiry & "#;"
' Run action query to restore data. Trap for errors,
' checking the Errors collection if necessary.
On Error GoTo Err_Execute
CurrentDb.Execute strSQLAppend, dbFailOnError
CurrentDb.Execute strSQLDelete, dbFailOnError
On Error GoTo 0
Exit Sub
Err_Execute:
' Notify user of any errors that result from
' executing the query.
If DBEngine.Errors.Count > 0 Then
For Each errLoop In DBEngine.Errors
MsgBox "Error number: " & errLoop.Number & vbCr & _
errLoop.Description
Next errLoop
End If
Resume Next
End Sub
Any help or guidance to resolve my problem would be extremely well appreciated - it would save me tearing from my hair out??? You've all been so very helpful to me in the past, and so I once again call upon your kind nature and
expertise to help me out of a potentially sticky situation....
Best Regards
CarolW
I was wondering if anyone could please help me with a SQL syntax issue that I am currently experiencing problems with.
I have a requirement to archive data over 2 years old, the data from which extracted from two tables on which I there is a one-to-many relationship. The PK from Table A is a National Insurance No which acts as the FK in Table B.
I managed to source some code (included below), which does a great job for one table (Table A), but I am unsure as to how I need to modify it in order to collect and archive its associated data in Table B. I have searched various forums for a solution but to no avail….
The fields in Table B are strNINo, strTrgProvider, strTrgActivity, strSector, strTutor and TrgCost
……………………………………………………………………………………………………..
What I Currently Have Is This:
Private Sub cmdArchiveData_Click()
'Run Archive - Append and Delete
Dim strSQLAppend As String
Dim strSQLDelete As String
Dim errLoop As Error
Dim dteExpiry As Date
dteExpiry = DateAdd("yyyy", -2, Date)
' Define two SQL statements for action queries.
strSQLAppend = "INSERT INTO tblExpiredStudents " & _
"( strNINo, strFirstName, strLastName, strAddress1, " & _
"strAddress2, strCity, strCounty, strPostCode, strTelephone, " & _
"hypE-mailAddress, dtmDOB, dtmEnrolled, strCourseID ) " & _
"SELECT tblStudentInformation.strNINo, " & _
"tblStudentInformation.strFirstName, " & _
"tblStudentInformation.strLastName, " & _
"tblStudentInformation.strAddress1, " & _
"tblStudentInformation.strAddress2, " & _
"tblStudentInformation.strCity, " & _
"tblStudentInformation.strCounty, " & _
"tblStudentInformation.strPostCode, " & _
"tblStudentInformation.strTelephone, " & _
"tblStudentInformation.E-mailAddress, " & _
"tblStudentInformation.dtmDOB, " & _
"tblStudentInformation.dtmEnrolled, " & _
"tblStudentInformation.strCourseID " & _
"FROM tblStudentInformation " & _
"WHERE tblStudentInformation.dtmEnrolled <= #" & dteExpiry & "#;"
strSQLDelete = "DELETE tblStudentInformation.strNINo, " & _
"tblStudentInformation.strFirstName, " & _
"tblStudentInformation.strLastName, " & _
"tblStudentInformation.strAddress1, " & _
"tblStudentInformation.strAddress2, " & _
"tblStudentInformation.strCity, " & _
"tblStudentInformation.strCounty, " & _
"tblStudentInformation.strPostCode, " & _
"tblStudentInformation.strTelephone, " & _
"tblStudentInformation.hypE-mailAddress, " & _
"tblStudentInformation.dtmDOB, " & _
"tblStudentInformation.dtmEnrolled, " & _
"tblStudentInformation.strCourseID " & _
"FROM tblStudentInformation " & _
"WHERE tblStudentInformation.dtmEnrolled <= #" & dteExpiry & "#;"
' Run action query to restore data. Trap for errors,
' checking the Errors collection if necessary.
On Error GoTo Err_Execute
CurrentDb.Execute strSQLAppend, dbFailOnError
CurrentDb.Execute strSQLDelete, dbFailOnError
On Error GoTo 0
Exit Sub
Err_Execute:
' Notify user of any errors that result from
' executing the query.
If DBEngine.Errors.Count > 0 Then
For Each errLoop In DBEngine.Errors
MsgBox "Error number: " & errLoop.Number & vbCr & _
errLoop.Description
Next errLoop
End If
Resume Next
End Sub
Any help or guidance to resolve my problem would be extremely well appreciated - it would save me tearing from my hair out??? You've all been so very helpful to me in the past, and so I once again call upon your kind nature and
Best Regards
CarolW