I'm plowing through lots of sample code for ideas and best practices.
This is one that I have a question about, and I see something similar in a lot of examples:
Notice the name for the error routine, "Form_Open_Err". Quite long and maybe meaningless.Code:Private Sub Form_Open(Cancel As Integer) On Error GoTo Form_Open_Err formtran Me Form_Open_Exit: Exit Sub Form_Open_Err: MsgBox Err.Description & " in Form_Open" Resume Form_Open_Exit End Sub
Either it was created automatically, or someone took a lot of time to name every error routine after the name of the procedure.
I question if that is necessary. Would something more generic be less ideal?
Is there a valid reason to NOT user the labels as I did in orange for all procedures in a module? (I think that's what they call subs and functions.)Code:Private Sub Form_Open(Cancel As Integer) On Error GoTo SubErr formtran Me SubExit: Exit Sub SubErr: MsgBox Err.Description & " in Form_Open" Resume SubExit End Sub
Even more valuable, why not call another subroutine to handle all errors? That way one set of code could display the same 'ol message box and maybe even log the error programmatically (and we could eliminate all that duplicate code in every procedure). Since in a error subroutine, we wouldn't need the SubExit:, would we just do a "Resume" to get back to the calling procedure that had the error? How do we just exit the calling sub with the error (if we use the subroutine model) so more undesired code doesn't run? Is there a variable the error subroutine could use to know the calling sub that had the error?
Or, does every procedure need a a duplicated error and exit section , and if we want to use a different subroutine for the msgbox, just add that to the SubErr: code?
Since the sub is private and contained, I can't see any problem with the code getting confused by the same label names, and it takes less bytes, and maybe the compiled version is more efficient/smaller without all the different label names?
Am I missing something?