[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

will END close all my recordsets and connections

In VB6 will the command END close all my recordsets and connections?  Or is it necessary to close all of them manually first.  I figure whats the big deal if I close my whole application by the command END.  Are there things still resident in memory afterwards?
0
al4629740
Asked:
al4629740
  • 7
  • 5
2 Solutions
 
Martin LissRetired ProgrammerCommented:
You should never use End. When you do VB doesn't get a chance to clean up after itself and a lot of things may be left in memory including a "shadow" of your running app.

What version of VB or VBA are you using?
0
 
al4629740Author Commented:
VB6..
0
 
Martin LissRetired ProgrammerCommented:
Then add a routine like this which will (as you can surly tell) will unload all your forms.
Public Sub UnloadAllForms()

    Dim frm As Form
    
    On Error GoTo ErrorRoutine
    
    For Each frm In Forms
        Unload frm
        Set frm = Nothing
    Next
    
    Exit Sub

ErrorRoutine:

    Msgbox "UnloadAllForms " & Err.Description
    
End Sub

Open in new window


and call it from your main form's QueryUnload event and/or any "Exit" type menu item you might have.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
al4629740Author Commented:
This will close all record sets properly and connections?
0
 
Martin LissRetired ProgrammerCommented:
Unless they are global in scope then I believe it will and if they are global you should disconnect from them in that procedure.
0
 
HooKooDooKuCommented:
Everything depends upon the scope of the record sets.

If the record sets are defined at the form level, then you could add to each form's Unload or Terminate event the logic to close the record set if it is open, and then set the record set to nothing.  Then something like Martin's idea to explicitly unload each form might work.  But even then, you might have to deal with some parent /child relationships depending upon  the complexity of your project.

Private m_rs As DAO.Recordset
Private Sub Form_Terminate()
    Call CloseRS
End Sub
Private Sub CloseRS()
    If Not m_rs Is Nothing Then
        Call m_rs.Close
        Set m_rs = Nothing
    End If
End Sub

Open in new window


But the way I liked to work was start every VB application with a Global.bas file.  I would declare my global forms and global objects in this file and always start in Sub Main().  I would then load the global objects and forms  and other program initialization  in Sub Main.  I would also create a subroutine in Global.bas called MainEnd().  The purpose of this subroutine was to unload the global forms and global objects.  Like the example above, I would simply look to see if a given object was not set to nothing.  That would indicate that at some point, the application had loaded the global object, so in MainEnd I would close it and set it back to Nothing.  That way, I could terminate my application at any point by calling MainEnd.  That would unload and terminate global objects and forms, and the Unload and Terminate events of these global objects would unload and terminate their child objects.
0
 
al4629740Author Commented:
I do have global variables.  Is there a simple routine that can set that all to nothing?
0
 
Martin LissRetired ProgrammerCommented:
If they are in a form, setting the form to Nothing will get rid of them. Otherwise you can create a procedure like this one and call it.

Public Sub SetToNothing()

Set x = Nothing
Set y = Nothing
'etc.
End Sub

Open in new window

0
 
al4629740Author Commented:
as hookoodooku indicated, do I need to close the recordsets?
0
 
Martin LissRetired ProgrammerCommented:
As long as you don't use End you can probably get away with not explicitly closing them, but it's good programming practice yo close them when you are done with them.
0
 
al4629740Author Commented:
What is the purpose of END anyways?
0
 
Martin LissRetired ProgrammerCommented:
It's hard to say. Maybe they wanted the programmer to have a way out of the code if he really, really doesn't care about the consequences of abruptly stopping VB.
0
 
Martin LissRetired ProgrammerCommented:
I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

Independent Software Vendors: 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!

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now