MS-Access,VBA和错误处理 [英] MS-Access, VBA and error handling

查看:291
本文介绍了MS-Access,VBA和错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个比一个真正的问题更多的观察:MS-Access(和一般的VBA)绝对缺少一个工具,其中可以自动生成错误处理代码,并且在发生错误时可以显示行号。你找到了解决方案吗?它是什么?我刚才意识到自从我在几年前找到了这个基本问题的正确答案之后,我幸免于难,我想看看你在这个非常重要的问题上的想法和解决方案。

解决方案

我的解决方案如下:


    安装 MZ-Tools ,这是一个非常有趣的VBA插件。不,他们没有支付我,无论如何它是免费的。
  1. 编程一个标准的错误处理程序代码,如这个(参见MZ工具菜单/选项/错误处理程序):






 发生错误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 

当然,一旦你意识到系统的兴趣,您可以想到一个更复杂的错误处理程序,它不仅会在调试窗口中显示数据,而且还将:


  1. 显示它作为屏幕上的消息

  2. 自动将错误日志文件中的一行插入错误
  3. 的描述
  4. 如果您使用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:

  1. install MZ-Tools, a very interesting add-on for VBA. No they did not pay me, anyway it is free.
  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 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:

  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?

这篇关于MS-Access,VBA和错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆