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
      .MoveFirst
      .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)

   SaveData

   rsCustomers.Close
   Set rsCustomers = Nothing
   SubFormUnload
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."

Why?

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)
  SaveData
End Sub

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

...but I get the same problem.
LVL 1
holgraveAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
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.

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
0
 
hmadhurCommented:
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?".

Regards
Madhur
0
 
PiterCommented:
Your last two lines are :

 "SubFormUnload"  ' what this means ?
 End Sub

instead  "Sub FormUnload" use "End"

just check ..




0
The new generation of project management tools

With monday.com’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.

 
TimCotteeCommented:
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.
0
 
holgraveAuthor Commented:
Woops! - duh!
0
 
PiterCommented:
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%      
SqlExit    
SqlWinExit    
X% = WinHelp(MDIMA.hWnd, "pubs1.hlp", HELP_QUIT, 0)

  End

End Sub

 Like this They Mention in MSDN.
But if you any any other procedure.. plz paste here.
 
0
 
TimCotteeCommented:
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
    Next

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.
0
All Courses

From novice to tech pro — start learning today.