Link to home
Start Free TrialLog in
Avatar of holgrave
holgraveFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hmadhur
hmadhur

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
Your last two lines are :

 "SubFormUnload"  ' what this means ?
 End Sub

instead  "Sub FormUnload" use "End"

just check ..




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.
Avatar of holgrave

ASKER

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