• Exiting a Parent sub from a child sub (Word 2000 VBA)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Exiting a Parent sub from a child sub (Word 2000 VBA)

    Author
    Topic
    #361033

    I don’t know if I have the terminology correct or not, but this is what I am trying to do. I have a bunch of what I’m calling “Parent subs”. One of the lines in the parent sub is to run what I am calling the “Child sub”. Within the Child sub, I have an If..Then. If the If condition is true, I want to exit both the child sub and the Parent sub that called it.

    I think I could do it by setting a variable, but I was hoping there was a simpler way to do it straight from the Child sub.

    The following is a generic example (where “….” means other code:

    Sub Parent()
    ….
    Child
    ….
    End Sub

    Public Sub Child()
    ….
    If X then
    Exit Parent sub
    Exit Child sub
    End If
    ….
    End sub

    Thanks for your help!!
    Troy

    Viewing 0 reply threads
    Author
    Replies
    • #544891

      It’s bad programming to do it that way. Instead, you should structure your subs so that the child routine either sets a module level flag or is a function that returns a value. Based on that flag or value, the parent routine will continue or exit. Otherwise, you lose control of cleanup procedures and may have unexpected results that are hard, if not impossible, to debug. Try something like this:

      Sub Parent()
      ....
          Result = Child()
          'The routine will only continue
          'if the Result = True
          If Result=True Then
      ....
          End If 
      End Sub
      
      Public Function Child()
      ....
          If X then
              Child = False
              Exit Function
          End If
      ....
      End Function
      • #544906

        I agree with Charlotte. I read some advice from Paul Lomax(author of the VB & VBA In A Nutshell) to stay way from using Subs and try to use more and more functions. Have the functions return true or false. It’s easier to control the flow.

        http://vb.oreilly.com/news/vb_tips_1098.html

      • #544971

        That seems to work fine (and thanks so much for pointing my in the right programming direction), EXCEPT why would my MsgBox appear twice? I have to click OK or hit ENTER twice to get rid of it. I have included the code I am using below.

        Thanks again for your help!!
        Troy

        Private Sub cbxAddCoverCopyright_Click()
        SetDocumentType
        Result = SetDocumentType()
        If Result = False Then
        cbxAddCoverCopyright.Value = False
        MsgBox “You must select a document type before setting options.”
        Cancel = True
        Exit Sub
        End If
        ….
        End Sub
        ——————————————————————————–
        Public Function SetDocumentType()
        If frmFormatDocumentMain.obBusinessDocument.Value = False And _
        frmFormatDocumentMain.obTechnicalDocument.Value = False And _
        frmFormatDocumentMain.obUserDocument.Value = False Then
        SetDocumentType = False
        End If
        End Function

        • #545001

          Why are you calling SetDocumentType twice, once as a sub and once as a function? And what is this supposed to do?

          cbxAddCoverCopyright.Value = False

          What kind of control is cbxAddCoverCopyright? If it’s a combobox, then there is no Cancel for the event, so Cancel = True doesn’t mean anything in that context. You’re probably getting the messagebox twice because your code line above its causing the routine to recurse and call itself. Setting the value to false is the same as clicking on it again.

          • #545015

            I’m fairly new at this stuff so bear with me. I’ll try to answer your questions amidst the code below:

            Private Sub cbxAddCoverCopyright_Click()

            ‘The following line of code is where I am calling the function for the first (and I thought only) time.
            SetDocumentType

            ‘The following line of code is where I am setting getting the Result of the function.
            Result = SetDocumentType()

            ‘Next I use the result in an If..Then statement.
            If Result = False Then

            “cbxAddCoverCopyright” is a check box. If setting the value to false is considered a click, HOW DO I GET AROUND THAT?

            cbxAddCoverCopyright.Value = False
            MsgBox “You must select a document type before setting options.”
            Cancel = True
            Exit Sub
            End If
            ….
            End Sub
            ——————————————————————————–
            Public Function SetDocumentType()
            If frmFormatDocumentMain.obBusinessDocument.Value = False And _
            frmFormatDocumentMain.obTechnicalDocument.Value = False And _
            frmFormatDocumentMain.obUserDocument.Value = False Then
            SetDocumentType = False
            End If
            End Function

            Thanks again for your help!!
            Troy

            • #545033

              Hi Troy,

              It looks like Charlotte is on the case, but here’s just a quick comment:

              The way your function is set up, there’s no way for it to ever return True.
              The details get a bit murky because you haven’t declared a type for the function – that is for instance:
              Set DocumentType() As Boolean
              so I think VBA is relying on some internal decision-making to assign a boolean type to the function (not sure, maybe that is the default type for a function if no type is assigned(?)).
              In any case, the default value for a boolean data type, whether declared as a function or as a variable, is False.
              So there’s nowhere in the function as it currently stands, where a True value can be assigned.
              Having a
              SetDocumentType = True
              as the first statement in the function might solve that.

              Also, if I may be excused a picky comment, it’s probably best to use a different word than “Set” in the name of the function – since Set has a special meaning it might lead to confusion later if you have a function called Set that doesn’t Set.

              Gary

            • #545036

              I guess I should have mentioned that this is but a small piece of a much larger project. The variable you mentioned is a public boolean variable in a separate module. I just forgot to include it with the code I posted.

              I’ll take into consideration getting rid of the “Set”. Thanks for the insightful input!!

              The main issue now is getting only one instance of the message box to show up. Everything else seems to work.

              Thanks for your help!!
              Troy

            • #545101

              You aren’t doing what you thought you were. Both lines call the SetDocumentType function. In the first case, you aren’t assigning the value to anything, you’re just calling the function as you would a subroutine. In the second, you’re calling it and assigning the result to a variable. You only need the second call.
              [indent]


              “cbxAddCoverCopyright” is a check box. If setting the value to false is considered a click, HOW DO I GET AROUND THAT?


              [/indent]I’m out of my territory here, because I don’t normally use forms in Word, only in Access and VB. Since you are seeing the messagebox twice, the only cause I could think of was that setting the value to False was triggering the Click event. I may be wrong, but I don’t see anything else that would cause it in the code you posted. What are you trying to accomplish by setting the combobox to false? Would setting the ListIndex to -1 work as well?

            • #545141

              What I was doing was bringing up a message box and deselecting a check box if one of three option buttons was not selected (they all start out false). The key to getting this down was your idea that setting the check box value to false was initiating the Click event. That is exactly what was happening. The following is my solution. Feel free to improve upon it, but this does work as I had hoped.

              NOTE: I did not include the function (which I changed to a sub) called “IsDocumentTypeSelected”. This function set’s a variable (DocumentTypeSelected) if it determines that none of the option buttons have been set to True.

              Private Sub cbxAddCoverCopyright_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
              IsDocumentTypeSelected
              If DocumentTypeSelected = False Then
              MsgBox “You must select a document type before setting options.”
              Cancel = True
              Exit Sub
              End If
              End Sub

              Private Sub cbxAddCoverCopyright_Click()
              If cbxAddCoverCopyright.Value = True Then
              frmOptionsCoverCopyright.Show
              PopulateCoverOptionControlsFromVariables
              End If
              End Sub

              By using the MouseDown event to check the status of the variable, I did not even have to set the checkbox value to false. I could then follow this with the Click event if the variable was true. For some strange but wonderful reason, if the MouseDown event found the variable to be false, the Click event never happened.

              Thanks for all the help!!
              Troy

    Viewing 0 reply threads
    Reply To: Exiting a Parent sub from a child sub (Word 2000 VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: