Saving Data in Unload Event

I have the following code in a form:

Option Explicit

Private rsCustomers As New ADODB.Recordset

Private Sub SaveData()
Dim lgCustomerID As Long

   'save the current record

   lgCustomerID = cmboCustomerCode.ItemData(cmboCustomerCode.ListIndex)
   With rsCustomers
      .Find "[CustomerID]=" & lgCustomerID
      !CustomerName = "" & txtCustomerName
      !WeekStartDay = "" & cmboWC
      !CustomerAdd1 = "" & txtAddress1
      !CustomerAdd2 = "" & txtAddress2
      !CustomerAdd3 = "" & txtAddress3
      !CustomerTown = "" & txtTown
      !CustomerCounty = "" & txtCounty
      !CustomerPostCode = "" & txtPostCode
      !CustomerContactName = "" & txtContactName
      !CustomerTelNo = "" & txtContactTelNo
      !CustomerFaxNo = "" & txtContactFaxNo
      !CustomerEmail = "" & txtEMail
      !CustomerWeb = "" & txtWeb
   End With

End Sub

Private Sub Form_Unload(Cancel As Integer)


   Set rsCustomers = Nothing
End Sub

However when I close the form (using the X in the top right of the forms Caption Bar) I get the following error on the rsCustomer.Close line:

"Run-timer error: '3219': Operation is not allowed in this context."


With the above code SaveData actually executes (the code steps through OK in debug) but the data is not actually saved!

I have also tried:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
End Sub

Private Sub Form_Unload(Cancel As Integer)
   Set rsCustomers = Nothing
End Sub

...but I get the same problem.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

TimCotteeConnect With a Mentor Commented:
Hi holgrave,

You don't seem to have a .Update statement anywhere in save data. The reason for the failure could be the pending update that is not committed.


Brainbench MVP for Visual Basic

Experts-Exchange Advisory Board Member
Hi TimCotte,

No wonder you are MVP for VB in Brainbench.

The first thing that came to mind after looking at the code was "Where is the update statement?".

Your last two lines are :

 "SubFormUnload"  ' what this means ?
 End Sub

instead  "Sub FormUnload" use "End"

just check ..

The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Piter, END is not a good thing. It doesn't allow VB to properly release memory and instantiated objects. It is generally accepted that using END is bad programming and you should ensure that all objects created are properly destroyed. End does not do this and may cause all sorts of problems.
holgraveAuthor Commented:
Woops! - duh!
hi TimCottee ,
 i think there is no other procedure to close MDI form.
 AS in MSDN they also mention the same procedure.

Sub MDIForm_Unload (Cancel As Integer)
' Clean up any connection with SQL Server and close
' the help system, then exit the application (End).
Dim X%      
X% = WinHelp(MDIMA.hWnd, "pubs1.hlp", HELP_QUIT, 0)


End Sub

 Like this They Mention in MSDN.
But if you any any other procedure.. plz paste here.
Piter, I am sure that it is shown in MSDN, that doesn't necessarily mean that this is the "best" way. There are other articles provided by Microsoft that explain why END is a bad thing.

To close an MDI_Form is actually no different than any other form. The key point is to ensure that any instantiated objects are properly destroyed. Now the problem with an MDI form is that you may have one or more children open and simply unloading without closing those forms first can leave the application still running or may simply fail.

What you need to do is to iterate the forms collection:

    Dim frmToClose As Form
    For Each frmToClose In Forms
        If Not (frmToClose Is Me) Then
            Unload frmToClose
        End If

This will close and unload all children of the MDI parent, you can also here ensure that any global objects are also properly destroyed.

Obviously within each form/class you should also ensure that any locally scoped objects are properly terminated.

This will mean that eventually you can simply unload the mdi form and you will know that you have properly terminated all objects and the application will then terminate gracefully without causing any memory leaks or other object related problems.
All Courses

From novice to tech pro — start learning today.