Results 1 to 6 of 6

Thread: exit sub

  1. #1
    Join Date
    Feb 2005
    Location
    The Hague, Netherlands
    Posts
    95

    Unanswered: exit sub

    How?

    I have a sub which retrieves a value from a combobox but when the value = "" I want to give a msgbox-message and exit the sub without executing the rest of the code

    I am a newbie, I dont know all the syntax yet

    More or less the same: Can you jump from code to code with some sort of 'goto' command? As in the old C64-days

  2. #2
    Join Date
    Sep 2003
    Location
    The extremely Royal borough of Kensington, London
    Posts
    778
    If me.comboBoxA = "someValue" Then
    MsgBox "MessageText"
    Exit Sub
    End If
    Bessie Braddock: Winston, you are drunk!
    Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.

  3. #3
    Join Date
    Feb 2005
    Location
    The Hague, Netherlands
    Posts
    95
    rofl exit sub is the actual command? thxz

  4. #4
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    you can goto... but nobody does that since the discovery of bronze, the wheel, fire and all those other modern technologies.

    outside of error handlers, goto is considered crap programming style

    private mySub()
    on error goto err_mySub
    'blah
    'blah
    'blah
    'blah
    'blah
    'blah
    exit_mySub:
    exit sub
    err_mySub:
    select case err.number
    case 99999
    'blah
    case 98765
    'blah
    case else
    msgbox err.number & ": " & err.description, vbcritical, "Unanticipated error in mySub"
    end select
    resume exit_mySub
    end sub

    izy
    currently using SS 2008R2

  5. #5
    Join Date
    Feb 2005
    Location
    The Hague, Netherlands
    Posts
    95
    Quote Originally Posted by izyrider
    you can goto... but nobody does that since the discovery of bronze, the wheel, fire and all those other modern technologies.

    outside of error handlers, goto is considered crap programming style

    private mySub()
    on error goto err_mySub
    'blah
    'blah
    'blah
    'blah
    'blah
    'blah
    exit_mySub:
    exit sub
    err_mySub:
    select case err.number
    case 99999
    'blah
    case 98765
    'blah
    case else
    msgbox err.number & ": " & err.description, vbcritical, "Unanticipated error in mySub"
    end select
    resume exit_mySub
    end sub

    izy
    Ok so whats the alternative then

  6. #6
    Join Date
    Nov 2004
    Location
    Bangor, ME USA
    Posts
    44
    If you make each subroutine a separate function, you can use

    Call Function_Name_of_sub

    then instead of exit sub in the errors or the subroutine of your msgbox use exit function.

    having a sub within a function, I have found doesn't work right on all occations and I have had it try to process a sub when not needed.

    Your best bet is to make a sub like Izyrider showed above.

    Good luck
    Kevin

Posting Permissions

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