Re: On Local Error Goto Somewhere



Hello, Lorin,

IMO the GoTo statement has never been "THE" problem. The problem is the "ComeFrom" statement -- i.e. the label. When I am looking at a GoTo, I know immediately where the next statement is. When I see a label, I don't have a clue how I got there. You have said this yourself in your comment

' label are cheap, and I can search to
find the source of this destination

The point is, that you have to search, and searching (my opinion) is not cheap.

Cheers,
Randy


Lorin wrote:

This brings up the discussion of GOTO since On Error GOTO is a GOTO.

This is the VB6 code I use on occassion if pressed

Public Function MySub(sMyFile as String) as String()
ON ERROR GOTO MySubErr Dim lFNbr as long
Dims sBuffer as String
Dim sArgs() as String
Dim iX as Integer

sBuffer = String$(50," ")
' some code
Do
100 Open sMyFile For Binary As lFnbr

200 Get lFNbr,, sBuffer ' what happens if there are not that many characters in the file?
If Split(sBuffer,",")(0) = "VALID" Then ' just making a point
sArgs=Split(sBuffer, ",") ' now get all
For iX = 0 to UBound(sArgs)
if sArg(iX) = "OOPS" then Exit Do ' a convieient was to escape using this GOTO
' Note it skips out of the FOR as well as the DO
if sArg(iX) = "GOSH" Then GoTo FlyingLeap ' another type of GOTO
' Now which is easier to follow?
' Can your eye find the label first or the end of the Loop?
' Labels are automatically left justified.
' what if this was in a complex multilayered nested bit of code?
Next iX
MySub = sArgs
EndIf
Loop While 0 ' solely the target of a GOTO, can you spot it?
' mixed message, I know, but what the heck.

FlyingLeap: ' label are cheap, and I can search to find the source of this destination
' some additional code goes here to do something very usefull

MySubExit:
Close lFnbr ' it always closes due to controlled exiting
' cleanup even from errors
Exit Sub

MySubErr:
Debug.Print Err.Description
Select case ERL
Case 100
' do something here in code
' if there is a possibility on an error at this point then call a subroutine with its own error handling to do the work and catch an error
Case 200
' do something else here in code
Case Else
' handle other errors here
End Select
' all errors resume to MySubExit
Resume MySubExit

End Sub ' MySub <---- do you know why I always do this?

I know, all you purists go HISSSS and BOOOO.
THis is not Spaghetti, just clear and responsible coding.

Any discussion????

Stirring the pot.




"Lorin" wrote:


Please compare and contrast this with
On Error Goto Somewhere

Big thnks!
.



Relevant Pages

  • Re: Error when running vb app with FlexGrid control
    ... Private Sub cmdNetChange_Click ... On Error GoTo Command1_Click_Error ... Private Sub fnGetData(strParam0 As String, strParam1 As String, FLX As ... Dim rsADOObject As Recordset ...
    (microsoft.public.vb.general.discussion)
  • Re: Automation of a mail merge in access 2003
    ... Message As String ... Dim objQDF As DAO.QueryDef ... Set objWORD = New Word.Application ... ' On Error GoTo ErrorHandler ...
    (microsoft.public.access.modulesdaovba)
  • Re: Command button problem
    ... Sorry about the goto, I know that's bad:) basically what I am trying ... Dim sServer As String, sUser As String ... Dim sNewPass As String, sOldPass As String ... sNewPass = StrConv(txtNew, vbUnicode) ...
    (microsoft.public.vb.general.discussion)
  • Re: Open MS Word in Access 2007
    ... and the Word doc opens with the data merged in. ... Dim xstring As String ... On Error GoTo err_get_cur_Pbomsword ... Dim inCriteria As String ...
    (microsoft.public.access.modulesdaovba)
  • Re: Open MS Word in Access 2007
    ... and the Word doc opens with the data merged in. ... Dim xstring As String ... On Error GoTo err_get_cur_Pbomsword ... Dim inCriteria As String ...
    (microsoft.public.access.modulesdaovba)