Automatically generating handling of issues

前端 未结 5 716
旧时难觅i
旧时难觅i 2020-11-27 18:53

This is more an observation than a real question: MS-Access (and VBA in general) is desperately missing a tool where error handling code can be generated automatically, and

5条回答
  •  无人及你
    2020-11-27 19:37

    My solution is the following:

    1. install MZ-Tools, a very interesting add-on for VBA. No they did not pay me to write this. Version 3 was free, but since version 8.0, the add-in is commercially sold.
    2. program a standard error handler code such as this one (see MZ-Tools menu/Options/Error handler):

    On Error GoTo {PROCEDURE_NAME}_Error
    {PROCEDURE_BODY}
    On Error GoTo 0
    Exit {PROCEDURE_TYPE}
    
    {PROCEDURE_NAME}_Error:
    debug.print "#" & Err.Number, Err.description, "l#" & erl, "{PROCEDURE_NAME}", "{MODULE_NAME}"
    

    This standard error code can be then automatically added to all of your procs and function by clicking on the corresponding button in the MZ-Tools menu. You'll notice that we refer here to a hidden and undocumented function in the VBA standard library, 'Erl', which stands for 'error line'. You got it! If you ask MZ-Tools to automatically number your lines of code, 'Erl' will then give you the number of the line where the error occured. You will have a complete description of the error in your immediate window, such as:

    #91, Object variable or With block variable not set, l# 30, addNewField, Utilities
    

    Of course, once you realize the interest of the system, you can think of a more sophisticated error handler, that will not only display the data in the debug window but will also:

    1. display it as a message on the screen
    2. Automatically insert a line in an error log file with the description of the error or
    3. if you are working with Access or if you are connected to a database, automatically add a record to a Tbl_Error table!

    meaning that each error generated at the user level can be stored either in a file or a table, somewhere on the machine or the network. Are we talking about building an automated error reporting system working with VBA?

提交回复
热议问题