Is it possible to pass a command from a function to a sub routine?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • isaace
    New Member
    • Jan 2012
    • 13

    Is it possible to pass a command from a function to a sub routine?

    I am trying to pass a command to a subroutine from a function and am having some difficulty. The purpose of the function is to validate that the lead is actually a client. If so, the sub can move forward. if not, I want to exit the sub from the function's code (the function is located in separate module, but I've still been successfully able to call it). When I enter 'exit sub' in the function, I get a database error. Can anyone help? other than that, the function works (i tested it by calling it from the sub). Also, if it is possible to pass a value to the sub from the function, I can then use than value in the sub to determine whether to finish executing it. Is it possible to pass a value from a function to a sub? It seams that when I store values to variables in the function and refer to them in the sub, they are all empty. Please help.
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    If you are in a function, then you would use Exit Function. Exit Sub is when you want to exit a sub-routine. If you are wanting to have the function exit the sub that called the function, then you need to change your design. What I would do is have a function that is declared as a boolean and all it would do is determine if the lead is as client. If the lead is a client, then it would return true. Otherwise it would return false. Then in the sub that called the function, you run a test on the value that it returns: if true, then continue, else, Exit Sub.

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      It is very poor codeing to exit from a called routine. In fact it is best practice to never exit from the called routine, either sub or function. Instead, return an error to the calling routine or have the calling routine check for a flag, work, etc.

      In this case, Functions are to be used to return a value, (IN best practice - always). Have the function return an invalid result code such a null, empty string, a negative number when only positives should be returned, etc... then have the calling code check the returned value and either continue execution or branch to the cleanup section and exit.

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        I can not possibly say it any better than zmbd. Here is a simple Example of how this approach would look:
        Code:
        Public Sub SomeSub()
        Dim strCName As String
        
        strCName = "Jesse James"
        
        If fTestForClient(strCName) Then
          MsgBox strCName & " is a valid Client"
        Else
          MsgBox strCName & " is an Imposter!"
        End If
        End Sub
        Code:
        Public Function fTestForClient(strClientName As String) As Boolean
          fTestForClient = DCount("*", "tblClients", "[ClientName] = '" & _
                           strClientName & "'") > 0
        End Function

        Comment

        Working...