Results 1 to 3 of 3

Thread: Exiting a program from inside a function - RESOLVED

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    43

    Resolved Exiting a program from inside a function - RESOLVED

    Hi all,

    Let's say I have a Sub Main procedure, that calls a function inside the Sub:

    Code:
    Sub Main ( )
    
         Call TheFunction(True)
    
    End Sub
    
    
    Function TheFunction(blnVar as Boolean)
    
         If blnVar = True Then
     
           ' I want to kill the Sub Main ( ) here!
    
         End If
    
    End Function
    I do realize that I can return a bool value from the function and then do a following check that if the return value is true then exit the Sub. I was just wondering if there is a way to kill the Sub Main from inside the Function.

    Sanctos
    Last edited by sanctos2003; Dec 2nd, 2004 at 12:06 PM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    By far the best method is as you said, return a value to say that the calling sub should exit, eg:
    VB Code:
    1. Sub Main ( )
    2.  
    3.      If TheFunction(True) Then
    4.        Exit Sub
    5.      End If
    6.  
    7. End Sub
    8.  
    9.  
    10. Function TheFunction(blnVar as Boolean) as Boolean
    11.  
    12.      If blnVar = True Then
    13.        TheFunction = True
    14.       End If
    15.  
    16. End Function

    You could use End, but this creates several problems (including wasting memory until you re-boot).

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2004
    Posts
    43
    Well I have a decent solution that requires only one line of code per function call that does everything at once:

    Code:
    Sub Main()
    
        Dim log As Object
        Set log = OpenLog("C:\Test.txt")
        
        If WriteLog(log, "Hello there!", True) = True Then Exit Sub
            
        If CloseLog(log) = True Then Exit Sub
    
    
    End Sub
    
    Function OpenLog(strFilePath As String) As TextStream
    
        Dim objFSO As New Scripting.FileSystemObject
        Dim objLogFile As TextStream
        Const ForAppending As Integer = 8
        
        If objFSO.FileExists(strFilePath) Then
            Set objLogFile = objFSO.OpenTextFile(strFilePath, ForAppending, True)
        Else
            Set objLogFile = objFSO.CreateTextFile(strFilePath, False)
        End If
        
        objLogFile.WriteLine "Log Opened: " & _
                             Format(Now, "mm/dd/yyyy") & " " & _
                             Format(Now, "hh:mm:ss AM/PM")
        
        Set OpenLog = objLogFile
            
    End Function
    
    
    Function WriteLog(objLogFile As TextStream, strMessage As String, blnExit As Boolean) As Boolean
        
        objLogFile.WriteLine strMessage & Chr(13)
            
        If blnExit = True Then
            WriteLog = True
        Else
            WriteLog = False
        End If
        
    End Function
    
    
    Function CloseLog(objLogFile As TextStream) As Boolean
    
        objLogFile.WriteLine "Log Closed: " & _
                             Format(Now, "mm/dd/yyyy") & " " & _
                             Format(Now, "hh:mm:ss AM/PM")
        objLogFile.Close
        Set objLogFile = Nothing
        
        CloseLog = True
        
    End Function
    Thanks for all your help,

    Sanctos

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