Results 1 to 9 of 9

Thread: using return in sub = exit sub

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2014
    Posts
    169

    using return in sub = exit sub

    if return in sub = exit sub

    example
    Code:
    If TextBox1.Text = "" Then
                ErrorProvider1.SetError(TextBox1, "empty")
                Return
            Else
                ErrorProvider1.SetError(TextBox1, "")
            End If

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: using return in sub = exit sub

    Yes... Return in a sub is the same as an Exit Sub... it's debateable if one is better over the other.

    BUT...
    as I mentioned in your other thread http://www.vbforums.com/showthread.p...-Provider-tool don't just set the errorprovider one at a time like that. If there are 5 fields that are in error, MARK THEM ALL ... don't make me hit hte button 5 times and report just one at a time. I find that annoying and I'm sure others do to.

    But, if you insist on doing it that way, then just use the if...elseif you originally had.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2014
    Posts
    169

    Re: using return in sub = exit sub

    Quote Originally Posted by techgnome View Post
    Yes... Return in a sub is the same as an Exit Sub... it's debateable if one is better over the other.

    BUT...
    as I mentioned in your other thread http://www.vbforums.com/showthread.p...-Provider-tool don't just set the errorprovider one at a time like that. If there are 5 fields that are in error, MARK THEM ALL ... don't make me hit hte button 5 times and report just one at a time. I find that annoying and I'm sure others do to.

    But, if you insist on doing it that way, then just use the if...elseif you originally had.

    -tg

    Code:
        If TextBox1.Text = "" Then
                ErrorProvider1.SetError(TextBox1, "empty")
                Return
            ElseIf TextBox2.Text = "" Then
                ErrorProvider1.SetError(TextBox2, "empty")
                Return
            Else
                ErrorProvider1.Clear()
            End If
    if i use elseif
    how to clear ErrorProvider1 so the ErrorProvider1 not appear for first textbox

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: using return in sub = exit sub

    Clear first... then set...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2014
    Posts
    169

    Re: using return in sub = exit sub

    Quote Originally Posted by techgnome View Post
    Clear first... then set...

    -tg
    work with me
    is this ok now
    Code:
     ErrorProvider1.Clear()
            If TextBox1.Text = "" Then
                ErrorProvider1.SetError(TextBox1, "empty")
                Return
            ElseIf TextBox2.Text = "" Then
                ErrorProvider1.SetError(TextBox2, "empty")
                Return
            End If

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: using return in sub = exit sub

    you tell me... if it works, and is what you want, then great. it's not how I would do it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: using return in sub = exit sub

    The point of using the If ElseIf is that you don't need the Returns really, because once one of the cases was true, it would skip the rest.
    Code:
            ErrorProvider1.Clear()
            If TextBox1.Text = "" Then
                ErrorProvider1.SetError(TextBox1, "empty")
            ElseIf TextBox2.Text = "" Then
                ErrorProvider1.SetError(TextBox2, "empty")
            End If

  8. #8
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: using return in sub = exit sub

    Quote Originally Posted by passel View Post
    The point of using the If ElseIf is that you don't need the Returns really, because once one of the cases was true, it would skip the rest.
    Code:
            ErrorProvider1.Clear()
            If TextBox1.Text = "" Then
                ErrorProvider1.SetError(TextBox1, "empty")
            ElseIf TextBox2.Text = "" Then
                ErrorProvider1.SetError(TextBox2, "empty")
            End If
    He should be checking all of them and flagging all of them each time anyways, using individual If's (not the ElseIf's):
    vb Code:
    1. ErrorProvider1.Clear()
    2. If TextBox1.Text.Trim = String.Empty Then
    3.     ErrorProvider1.SetError(TextBox1, "empty")
    4. End If
    5. If TextBox2.Text.Trim = String.Empty Then
    6.     ErrorProvider1.SetError(TextBox2, "empty")
    7. End If
    Or do it with a simple loop:
    vb Code:
    1. ErrorProvider1.Clear()
    2. Dim tb As TextBox
    3. For Each ctrl As Control In Me.Controls
    4.     If TypeOf ctrl Is TextBox Then
    5.         tb = DirectCast(ctrl, TextBox)
    6.         If tb.Text.Trim = String.Empty Then ErrorProvider1.SetError(tb, "empty")
    7.     End If
    8. Next
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: using return in sub = exit sub

    Quote Originally Posted by JuggaloBrotha View Post
    He should be checking all of them and flagging all of them each time anyways, using individual If's (not the ElseIf's):
    vb Code:
    1. ErrorProvider1.Clear()
    2. If TextBox1.Text.Trim = String.Empty Then
    3.     ErrorProvider1.SetError(TextBox1, "empty")
    4. End If
    5. If TextBox2.Text.Trim = String.Empty Then
    6.     ErrorProvider1.SetError(TextBox2, "empty")
    7. End If
    Or do it with a simple loop:
    vb Code:
    1. ErrorProvider1.Clear()
    2. Dim tb As TextBox
    3. For Each ctrl As Control In Me.Controls
    4.     If TypeOf ctrl Is TextBox Then
    5.         tb = DirectCast(ctrl, TextBox)
    6.         If tb.Text.Trim = String.Empty Then ErrorProvider1.SetError(tb, "empty")
    7.     End If
    8. Next
    Exactly! that's what I said in the other thread as well... but for what ever reason, he's hell bent on doing it this way. I've given up... something about horses and water.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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