MS-Access,VBA和错误处理 [英] MS-Access, VBA and error handling
问题描述
我的解决方案如下:
- 编程一个标准的错误处理程序代码,如这个(参见MZ工具菜单/选项/错误处理程序):
安装 MZ-Tools ,这是一个非常有趣的VBA插件。不,他们没有支付我,无论如何它是免费的。
发生错误GoTo {PROCEDURE_NAME} _Error
{ PROCEDURE_BODY}
出错GoTo 0
退出{PROCEDURE_TYPE}
{PROCEDURE_NAME} _Error:
debug.print#& Err.Number,Err.description,l#& erl,{PROCEDURE_NAME},{MODULE_NAME}
然后,可以通过单击MZ-Tools菜单中的相应按钮将此标准错误代码自动添加到所有的procs和函数中。你会注意到我们在这里提到一个VBA(2003版)的无证值/属性'erl',它代表'错误行'。你说对了!如果您要求MZ-Tools自动对您的代码行进行编号,那么erl将会给出错误发生的行号。您将对您的即时窗口中的错误进行完整的描述,例如:
#91,对象变量或块变量没有设置,l#30,addNewField,Utilities
当然,一旦你意识到系统的兴趣,您可以想到一个更复杂的错误处理程序,它不仅会在调试窗口中显示数据,而且还将:
- 显示它作为屏幕上的消息
- 自动将错误日志文件中的一行插入错误或 的描述
- 如果您使用Access或连接到数据库,请自动将记录添加到Tbl_Error表中!
意味着在用户级别生成的每个错误都可以存储在文件或表中,机器或网络上的某个位置。我们在谈论使用VBA构建自动化错误报告系统?
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 where the line number can be displayed when an error occurs. Did you find a solution? What is it? I just realized how many hundreds of hours I spared since I found the right answer to this basic problem a few years ago, and I'd like to see what are your ideas and solutions on this very important issue.
My solution is the following:
- install MZ-Tools, a very interesting add-on for VBA. No they did not pay me, anyway it is free.
- 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 an undocumented value/property of VBA (2003 edition), '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:
- display it as a message on the screen
- Automatically insert a line in an error log file with the description of the error or
- 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?
这篇关于MS-Access,VBA和错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!