I have a series of forms that allow the users to update data in a temporary table and then delete the records in the permanent table and append the records from the temporary table to the permanent table. The final step is to delete the temporary table. I have everything working up to the deletion of the temporary table. I am receiving a run-time error '3211': The database engine could not lock table 'Temporary_1' because it is already in use by another person or process.
The temporary table is the source for the form that the user updates and this command runs from. So, the last thing we do before attempting to delete the temporary table is close the form. I thought maybe the issue is that we use the temporary table in our recordset so, we would need to close the record set before deleting the table, but when I try to do this I get a Run-time error '3704' Operation is not allowed when the object is closed. This confuses me because I open the recordset and do not close it, why would it not need to be closed?
I do not see anywhere else in my code or my forms that the temporary table is being used and should be locked. Any advice is greatly appreciated.
The temporary table is the source for the form that the user updates and this command runs from. So, the last thing we do before attempting to delete the temporary table is close the form. I thought maybe the issue is that we use the temporary table in our recordset so, we would need to close the record set before deleting the table, but when I try to do this I get a Run-time error '3704' Operation is not allowed when the object is closed. This confuses me because I open the recordset and do not close it, why would it not need to be closed?
I do not see anywhere else in my code or my forms that the temporary table is being used and should be locked. Any advice is greatly appreciated.
Code:
Private Sub cmdTest_Click()
Dim conn As ADODB.Connection
Dim stPath As String
Dim rst As ADODB.Recordset
Dim sSQL As String
Dim rst1 As ADODB.Recordset
Dim sSQL1 As String
Dim dblRptOwnr As Double
Dim stRptTitle As String
Dim stTblName As String
dblRptOwnr = [Forms]![frmDialogMngrSbprjtRptGrpSelect]![cmbRptOwnr]
stRptTitle = [Forms]![frmDialogMngrSbprjtRptGrpSelect]![cmbRptTitle]
stTblName = "TBLMNGRSBPRJTRPTDETAILS_" & GetSBPRJTRPTGRPID(dblRptOwnr, stRptTitle)
' Set the string to the path of your database
stPath = CurrentDb.Name
Debug.Print stPath
' Open connection to the database
Set conn = New ADODB.Connection
conn.Provider = "Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & stPath & ";"
conn.Open
'Select the Current MNGRSBPRJTRPTGRPID Records in TBLMNGRSBORJTRPTDETAILS
sSQL = "SELECT * " _
& " FROM TBLMNGRSBPRJTRPTDETAILS " _
& " WHERE (((TBLMNGRSBPRJTRPTDETAILS.SBPRJTRPTGRPID)=" & [Forms]![frmDialogMngrSbprjtRptGrp]![txtRptGrpID] & "));"
Set rst = New ADODB.Recordset
rst.Open sSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
'Delete all of the currently selected MNGRSBPRJTRPTGRPID Records in TBLMNGRSBORJTRPTDETAILS
With rst
Do While Not .EOF
Debug.Print rst!SBPRJTRPTGRPID & ", " & rst!SubprojectID
.Delete
.MoveNext
Loop
End With
'Append all of the records from the Temp TBLMNGRSBPRJTRPTDETAIL_MNGRSBPRJTRPTGRPID table
sSQL1 = "INSERT INTO TBLMNGRSBPRJTRPTDETAILS ( SBPRJTRPTGRPID, SUBPROJECTID, ADD_BY, ADD_DTTM, MOD_BY, MOD_DTTM )" _
& " SELECT SBPRJTRPTGRPID, SUBPROJECTID, ADD_BY, ADD_DTTM, MOD_BY, MOD_DTTM" _
& " FROM " & stTblName _
& " WHERE " & stTblName & "!FLAG = No"
Debug.Print sSQL1
Set rst1 = New ADODB.Recordset
Debug.Print rst1.State
rst1.Open sSQL1, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
Debug.Print rst1.State
DoCmd.Close 'Close the form based on the temporary table
rst.Close
Set rst = Nothing
Debug.Print rst1.State
rst1.Close 'Receiving Run-time error '3704' Operation is not allowed when the object is closed.
'This confuses me because I open the recordset and do not close it, why would it not need to be closed?
Set rst1 = Nothing
DoCmd.DeleteObject acTable, stTblName 'Receiving Run-time error '3211':
'The database engine could not lock table 'Temporary_1' because it is already in use by another person or process.
End Sub
Comment