Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Saving Data in Unload Event

Posted on 2003-03-20
7
Medium Priority
?
172 Views
Last Modified: 2010-04-07
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.
0
Comment
Question by:holgrave
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 1000 total points
ID: 8172958
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
 
LVL 1

Expert Comment

by:hmadhur
ID: 8173024
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
 
LVL 1

Expert Comment

by:Piter
ID: 8173129
Your last two lines are :

 "SubFormUnload"  ' what this means ?
 End Sub

instead  "Sub FormUnload" use "End"

just check ..




0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Expert Comment

by:TimCottee
ID: 8173158
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
 
LVL 1

Author Comment

by:holgrave
ID: 8174853
Woops! - duh!
0
 
LVL 1

Expert Comment

by:Piter
ID: 8179348
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
 
LVL 43

Expert Comment

by:TimCottee
ID: 8179554
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question