dcsimg
Results 1 to 8 of 8

Thread: [RESOLVED] VB6 - Terminating VB after program execution

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    341

    Resolved [RESOLVED] VB6 - Terminating VB after program execution


    Hello good people,

    I need some clarification on one minor issue with VB. My programs runs okay but after the program execution and "Exit sub", the program is still not done. I have to manually go to code area to click on Run/End to terminate the program.

    Should I let it go to "End Sub"?

    The reason I am raising this issue is because yesterday, I ran my program after making some changes. When I went to run the program again, it says "file is already open". I had to manually end the program then the error went away.

    Question: what is the appropriate way to terminate the program after it completed execution and "Exit Sub"?
    Thanks.

    Below is where I am ending my program after successful execution:
    Code:
    'unload/terminate the ftp object
    Set cFTP = Nothing
    Set txtfl = Nothing
    
    Screen.MousePointer = vbDefault
    
    Exit Sub
    
    Errorhandler:
            MsgBox Str(Err.Number) & ": " & Err.Description, , "Error"
    
    End Sub

    Giftx.

  2. #2
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: VB6 - Terminating VB after program execution

    do you have any unload commands for unloading any of your forms when you are done...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


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

    Re: VB6 - Terminating VB after program execution

    Letting it go to "End Sub" is not relevant, what matters is that you are tidying up properly (not just in the main body of the sub, but inside your error handler too).

    For an explanation (and example code) for how you should be closing a program, see the FAQ article How should I close my form/program/class?

  4. #4
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    Re: VB6 - Terminating VB after program execution

    What matters at this point is what code has CALLED this routine that you pasted. Depending on what the caller is doing after this is what we need to know. so if you could paste that it would help.

  5. #5
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    Pakistan
    Posts
    388

    Re: VB6 - Terminating VB after program execution

    If you want the application to quit, just use

    Unload Me
    instead of Exit Sub when you are done. Also use Unload Me at the end of your ErrorHandler: commands. This command (Unload Me) will unload and terminate your application (if it is built on a single form) cleanly. Should come in handy.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    341

    Re: VB6 - Terminating VB after program execution

    Danecook21,
    this my entire code:
    Note: My FTP definitions, variable and functions called are in a class module called clsFTP and all calls a terminated properly.

    Code:
    'On Error GoTo Errorhandler
    
    Dim FTPCompleted As Boolean
    Dim RecCtr As Integer, FTPRestart As Integer, MessagesFound As Integer
    Dim dbUnclaimed As DAO.Database
    Dim rsMsg As DAO.Recordset
    Dim rsFTP As DAO.Recordset
    Dim FTPInfo As String
    Dim txtfl As Scripting.TextStream
    'Dim cFTP As clsFTP GX *******
    Dim cFTP As Object
    Dim bSuccess As Boolean
    Dim sError As String
    Dim ftp As String ' New chilkatFtp2 GX*********
    Dim success As Long
    Dim LocalFilename As String
    Dim RemoteFilename As String
    Dim SourceFilePath As String
    Dim RetVal
    Dim FSO As New FileSystemObject
    
    Dim sMessage As String
    
    Screen.MousePointer = vbHourglass
    
    'SourceFilePath = "\\Livctrls03-08\Data1\APP\UNCLAIMW\UCCTest\Databases\DataFiles\"
    
    
        'ProgressBar1.Value = 10
        FTPRestart = 0
        FTPCompleted = False
        
        'Set Access to Database:
        '-------------
        Set dbUnclaimed = OpenDatabase("\\Livctrls03-08\Data1\APP\UNCLAIMW\UCCTest\Databases\Unclaimed Checks.mdb")
        
        'RETRIEVE EMPLOYEES UNCLAIMED CHECK INFORMATION FROM UNCLAIMED CHECK TABLE(.mdb).
        '--------------------------------------------------------------------------------
        Set rsMsg = dbUnclaimed.OpenRecordset("SELECT DISTINCT tblUnclaimed.L1L2, PassNumber FROM tblUnclaimed LEFT JOIN EmployeeInfo ON (tblUnclaimed.L1L2 = EmployeeInfo.L1L2) AND (tblUnclaimed.PassNumber = EmployeeInfo.Pass_Number) WHERE (tblUnclaimed.Status IN ('A','E','U') AND Len([EmployeeInfo].[Status]) = 1) ORDER BY tblUnclaimed.L1L2, tblUnclaimed.PassNumber;")
    
    RetryFTP:
        
        'CREATE A TEXT FILE - THIS WILL BE THE DEPOSITORY FOR THE MESSAGES THAT WILL CREATED IN THE NEXT STEP:
        '-----------------------------------------------------------------------------------------------------
        LocalFilename = "Emp_Messages_" & Format(Time(), "hhmm") & ".txt"
        RemoteFilename = LocalFilename
        
        Set txtfl = FSO.CreateTextFile("\\Livctrls03-08\Data1\APP\UNCLAIMW\UCCTest\Databases\DataFiles\" & LocalFilename, True)
                                                                                                       
        'ProgressBar1.Value = 20
        
        'CREATE FILE - CONTAINING MESSAGES THAT WILL BE DISPLAYED ON EMPLOYEES CHECKS:
        '----------------------------------------------------------------------------
        Do Until rsMsg.EOF
            sMessage = "ZN" & rsMsg.Fields("L1L2") _
            & VBA.Format(rsMsg.Fields("PassNumber"), "0000000000") _
            & "  CONTACT YOUR TIMEKEEPER - YOU MAY HAVE AN UNCLAIMED CHECK"
            txtfl.WriteLine (sMessage)
            rsMsg.MoveNext
        Loop
        
        txtfl.Close
        
        
        LocalFilename = "\\Livctrls03-08\Data1\APP\UNCLAIMW\UCCTest\Databases\DataFiles\" & LocalFilename
        
         
    'This loads/initialises the FTP object
    Set cFTP = New clsFTP
    
    If cFTP.OpenConnection("10.8.37.44", "mfuchk", "58UA?UUW") Then
        
         bSuccess = cFTP.FTPUploadFile(LocalFilename, RemoteFilename)
        
        If bSuccess Then
            sError = "FTP - File Upload to Server was Successful"
        Else
            sError = cFTP.SimpleLastErrorMessage
        End If
        
        cFTP.CloseConnection
    Else
        sError = cFTP.SimpleLastErrorMessage
    End If
    
    MsgBox sError
    
    'unload/terminate the ftp object
    Set cFTP = Nothing
    Set txtfl = Nothing
    
    Screen.MousePointer = vbDefault
    
    Exit Sub
    
    Errorhandler:
            MsgBox Str(Err.Number) & ": " & Err.Description, , "Error"
    
    End Sub
    Thanks.

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

    Re: VB6 - Terminating VB after program execution

    You aren't closing your object variables properly (ones where you use "Set"), even outside of the error handler.

    For example, with rsMsg you should tell it to close and then disconnect from it, eg:
    Code:
    rsMsg.Close
    Set rsMsg = Nothing
    You should also do it in the error handler, but that is more complex as you don't know how much of the code has run - so you don't know if the objects have even been set up yet.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    341

    Re: VB6 - Terminating VB after program execution

    Okay guys. I got it. so every object that was "set to Something" must be "closed" and also "set to Nothing" after execution.

    Thanks y'all.
    Giftx.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width