Results 1 to 16 of 16

Thread: [RESOLVED] How to close the program

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Resolved [RESOLVED] How to close the program

    I have confuse between "unload me" and "End" . What is best way to close the program running from executable file?Close the program mean to close the program and clear all the memory used by the program.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to close the program

    Without exception, use "Unload Me", and in the Form_Unload event of each form tidy up as needed (such as closing objects, etc).

    Don't use "End", as what that basically does is intentionally crash your program (but without any error messages). None of the code in your _Unload (or_QueryUnload) events will fire, any objects you have open (such as database connections) will not close, etc.

    While "End" does some very minimal clean-up, lots of things aren't tidied up - which means your program will probably still be using memory, and might do damage to whatever it was working with.


    I am in the process of writing an FAQ article on this subject, but may not have it finished for a few more days.

  3. #3
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Re: How to close the program

    I used to have something like this (Don't remember exactly)
    Code:
    Private Sub CloseProgram()
        While Forms.Count > 1
            Unload Forms(Forms.Count - 1)
            DoEvents
        End While
        Unload Me
    End Sub
    If you call this code from a form, this loop will keep open that form, so Forms.Count > 1 does the trick. If you're in a procedure like Sub Main and this code is NOT run inside a form then you can also change it to 0.

    Best regards,
    Xmas.
    Learn, this is the Keyword...

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: How to close the program

    Actually, I don't know what should I put in the close command button either unload me or end If I running from executable file.

    That mean when I close the program, it clear all the memory used for run the program and also the database.

    If I used unload me, it is it close all the form including hide form and so on? I want to close all the hiden form and the open up form as well.

  5. #5
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Re: How to close the program

    Quote Originally Posted by matrik02
    Actually, I don't know what should I put in the close command button either unload me or end If I running from executable file.

    That mean when I close the program, it clear all the memory used for run the program and also the database.

    If I used unload me, it is it close all the form including hide form and so on? I want to close all the hiden form and the open up form as well.
    If you use "Unload Me", it will unload "me"... So if this is the last form the program will close too. If you have hidden forms, well I'm sorry, that forms will still be there. The code I posted before should do the trick.

    Best regards,
    Xmas.
    Learn, this is the Keyword...

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: How to close the program

    End While if error.

    If I used your code, you code could unload all the open up forms and hidden form as well?

  7. #7
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Re: How to close the program

    Sorry. VB6 uses While/Wend and not While/End While

    Yes, you unload hidden forms as well.
    Learn, this is the Keyword...

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: How to close the program

    Thank you I have try it and it works..

    But When I have two forms.. Why End button it still active?


    When I have only one form, the end button is unactive (that what I want if I have many hidden and open up form.)
    Attached Images Attached Images  

  9. #9
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Re: How to close the program

    This way:
    Code:
        While Forms.Count > 0
            Unload Forms(Forms.Count - 1)
            DoEvents
        Wend
        Unload Me
    Here's an example project.
    Attached Files Attached Files
    Learn, this is the Keyword...

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: How to close the program

    Thank you alot..
    But I also have database, What the best way to close the database to prevent from damage?

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to close the program

    That depends on how you work with the database.

    Assuming you are using ADO code, you would use code like this for every recordset/command object:
    Code:
      If (variable.State And adStateOpen) = adStateOpen Then variable.Close 
      Set variable = Nothing
    ..and then do the same for the Connection object.


    Note that it is best to close any recordset and command objects as soon as you have finished using them, rather than waiting until _Unload.

    If you need to keep them open as long as your program is running (like if various parts of the program use the same recordset without re-opening it), then you will need to wait until _Unload.

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: How to close the program

    Why I got error message like this

    Object Variable not Set

    Code:
      If consys.State = adStateOpen Or con.State = adStateOpen Then
       con.Close
       consys.Close
       Set consys = Nothing
       Set con = Nothing
       End If
      
        
        While Forms.Count > 0
            Unload Forms(Forms.Count - 1)
            DoEvents
        Wend
        Unload Me

  13. #13
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Re: How to close the program

    In which line?
    Learn, this is the Keyword...

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: How to close the program

    Ok, I solve this, consys actually in other form.

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to close the program

    I'm afraid that this block of code is not good enough:
    Quote Originally Posted by matrik02
    Code:
      If consys.State = adStateOpen Or con.State = adStateOpen Then
       con.Close
       consys.Close
       Set consys = Nothing
       Set con = Nothing
       End If
    First of all, you should not check the .State property of any ADO objects directly against a specific value, as it can have more than one value at a time (such as "open" and "getting data"). Instead, you should use a bit mask like in my example.

    Next up, you will get errors if one of those objects is open but the other isn't (as you will try to close the one which is already closed). To deal with it correctly, you should have separate If statements for each one, with just the code to close that particular object.

    Finally, you should always set your objects to Nothing.. you cannot do any damage by doing that (even if it has been done already), so put those lines outside the If blocks.

    Making those changes means the code would be like this:
    Code:
      If (consys.State And adStateOpen) = adStateOpen Then
        consys.Close 
      End If
      Set consys = Nothing
      If (con.State And adStateOpen) = adStateOpen Then 
        con.Close 
      End If
      Set con = Nothing
    ..or like this:
    Code:
      If (consys.State And adStateOpen) = adStateOpen Then consys.Close 
      Set consys = Nothing
      If (con.State And adStateOpen) = adStateOpen Then con.Close 
      Set con = Nothing
    You may find it useful to read this post (from the Database FAQ article "What does this error mean, and how do I fix it?"), which contains a bit more information.

  16. #16

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: [RESOLVED] How to close the program

    Oh thank you so much for making some correction in code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width