FancyPrairie
Programmer
I'm trying to archive some data. Data in 3 live tables need to be copied to 3 archive tables. After the data has been copied the data in each of the 3 live tables will be deleted. I want to wrap all 6 SQL statements within a transaction. I can't get the following to work. If I copy and paste the SQL statement into a query and run the query, it works fine. However, if I execute the SQL statement or the query via code, I get an error. What am I doing wrong?
Example 1:
(Error message: You cannot add or change a record because a related record is required in Table abc) Note that there is no relationship with table abc and, if I copy and paste the SQL statement into a new query and run the query, it works fine)
Example 2 (not sure how to set transaction here...doesn't work as written anyway):
Example 3 (works but can't emcompass all 6 in one Transaction):
Example 1:
(Error message: You cannot add or change a record because a related record is required in Table abc) Note that there is no relationship with table abc and, if I copy and paste the SQL statement into a new query and run the query, it works fine)
Code:
Dim cnn As ADODB.Connection
Dim strSQL As String
strSQL = "INSERT INTO tblPatient_copyto_Admission "
strSQL = strSQL & "SELECT lngAccount, lngMRN, dteAdmit, dteDischarge, strService, strPatientLoc, strRoomBed, ysnIsolation, ysnOptOut, dtmAdded, dtmModified, strModifiedBy, ArchivedDateTime() AS dtmArchived "
strSQL = strSQL & "FROM tblPatient_copyfrom_Admission "
strSQL = strSQL & "WHERE ((Not (tblPatient_copyfrom_Admission.dteDischarge) Is Null)) AND (tblPatient_copyfrom_Admission.dteDischarge < #1/1/2005#);"
strSQL = Replace(strSQL, "copyto", "Archive")
strSQL = Replace(strSQL, "copyfrom", "Live")
Set cnn = CurrentProject.Connection
cnn.BeginTrans
cnn.Execute strSQL
Example 2 (not sure how to set transaction here...doesn't work as written anyway):
Code:
Dim strSQL As String
Dim cmd As ADODB.Command
strSQL = "INSERT INTO tblPatient_copyto_Admission "
strSQL = strSQL & "SELECT lngAccount, lngMRN, dteAdmit, dteDischarge, strService, strPatientLoc, strRoomBed, ysnIsolation, ysnOptOut, dtmAdded, dtmModified, strModifiedBy, ArchivedDateTime() AS dtmArchived "
strSQL = strSQL & "FROM tblPatient_copyfrom_Admission "
strSQL = strSQL & "WHERE ((Not (tblPatient_copyfrom_Admission.dteDischarge) Is Null)) AND (tblPatient_copyfrom_Admission.dteDischarge < #1/1/2005#);"
strSQL = Replace(strSQL, "copyto", "Archive")
strSQL = Replace(strSQL, "copyfrom", "Live")
Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandType = adCmdText
cmd.CommandText = strSQL
cmd.Execute
Example 3 (works but can't emcompass all 6 in one Transaction):
Code:
Dim strSQL As String
strSQL = "INSERT INTO tblPatient_copyto_Admission "
strSQL = strSQL & "SELECT lngAccount, lngMRN, dteAdmit, dteDischarge, strService, strPatientLoc, strRoomBed, ysnIsolation, ysnOptOut, dtmAdded, dtmModified, strModifiedBy, ArchivedDateTime() AS dtmArchived "
strSQL = strSQL & "FROM tblPatient_copyfrom_Admission "
strSQL = strSQL & "WHERE ((Not (tblPatient_copyfrom_Admission.dteDischarge) Is Null)) AND (tblPatient_copyfrom_Admission.dteDischarge < #1/1/2005#);"
strSQL = Replace(strSQL, "copyto", "Archive")
strSQL = Replace(strSQL, "copyfrom", "Live")
DoCmd.RunSQL strSQL, True