what's the difference between
andCode:on error goto ErrH:
Code:on error goto ErrH
Printable View
what's the difference between
andCode:on error goto ErrH:
Code:on error goto ErrH
I believe on Error goto ErrH: is a label
on error goto Errh is a statement.
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.
Both are supported, and both will work.
The colon typically, however, denotes a label to which the sub or function should branch.
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.Quote:
Originally posted by vbgladiator
I believe on Error goto ErrH: is a label
on error goto Errh is a statement.
So this is legal?
On error goto ErrHandler:
ErrHandler:
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 projectYou will get the Division by zero message box. Now run thisVB Code:
Private Sub Command1_Click() On Error GoTo errh Dim i As Integer i = 1 / 0 errh: MsgBox "Division by zero" End SubOnce again, you will get the Division by zero message box.VB Code:
Private Sub Command1_Click() On Error GoTo errh: Dim i As Integer i = 1 / 0 errh: MsgBox "Division by zero" End Sub
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
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.
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.Quote:
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