Results 1 to 10 of 10

Thread: on error goto

  1. #1

    Thread Starter
    Addicted Member c@lle's Avatar
    Join Date
    Oct 1999
    Location
    Belgium
    Posts
    179

    on error goto

    what's the difference between
    Code:
    on error goto ErrH:
    and

    Code:
    on error goto ErrH

  2. #2
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950
    I believe on Error goto ErrH: is a label

    on error goto Errh is a statement.

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    None!
    A label that you jump to must look like this.
    LabelName:
    With the colon but at the goto part the colon can be omitted.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Both are supported, and both will work.

    The colon typically, however, denotes a label to which the sub or function should branch.

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by vbgladiator
    I believe on Error goto ErrH: is a label

    on error goto Errh is a statement.
    On Error Goto is a statement and with goto you have to name the label. But the colon is only necassary to define the actual label.

  6. #6
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950
    So this is legal?

    On error goto ErrHandler:


    ErrHandler:

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    I ran a test before posting, because I wasn't sure if using a colon in the On Error would work, but it does. Run this in a test project
    VB Code:
    1. Private Sub Command1_Click()
    2.     On Error GoTo errh
    3.     Dim i As Integer
    4.     i = 1 / 0    
    5. errh:
    6.    MsgBox "Division by zero"
    7. End Sub
    You will get the Division by zero message box. Now run this
    VB Code:
    1. Private Sub Command1_Click()
    2.     On Error GoTo errh:
    3.     Dim i As Integer
    4.     i = 1 / 0    
    5. errh:
    6.    MsgBox "Division by zero"
    7. End Sub
    Once again, you will get the Division by zero message box.

  8. #8
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    If you want to string multiple lines of code together into one long line of code you can use the full colon to put them together.
    So I suppose you could probably stick a colon after anything and it would work the same
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Well actually Hack you'll get that message box even if you remove the division statement... You'll always need to exit the sub or function before the Label so the statements in the error handler is only executed if an error has occured.

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by plenderj
    If you want to string multiple lines of code together into one long line of code you can use the full colon to put them together.
    So I suppose you could probably stick a colon after anything and it would work the same
    That's true. But the actual definition of the Label must be followed by a colon. That also proves that you can't use a keyword or a function name as a label.

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