Closing an open recordset creates a runtime error

I have an ADO record set variable that is declared in a class module of a custom form. The recordset opens in the "Activate event" form, and I'm trying to close it in the "Finish" form with code like this:

Private Sub UserForm_Terminate() If VersionIsReleased Then ThisWorkbook.Parent.Quit Else If Not m_rs Is Nothing Then If m_rs.State = adStateOpen Then m_rs.Close End If Set m_rs = Nothing End If Close_CN g_cn ThisWorkbook.Application.Visible = True End If End Sub 

The line m_rs.Close generates a run-time error: "Operation is not allowed in this context." Any ideas why this is happening?

+4
source share
1 answer

This error may occur if the recordset is being edited while trying to close it. Make sure that if you use AddNew or change any Fields().Value that you use Update to save the changes before closing. You can check the EditMode property to see what state the recordset is in.

+4
source

All Articles