vba 在 MS Access 中记录错误和警告的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8823259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Method for logging errors and warnings in MS Access
提问by Tom
I'm an intern who is making a billing database for a new market that my company is in. I have created all the tables, and have set up an automatic way to grab and import the data. However, the method of importing is sort of brute force and not very elegant, because I've only had like 2 weeks to work on it.
我是一名实习生,正在为我公司所在的新市场制作计费数据库。我已经创建了所有表,并设置了一种自动获取和导入数据的方法。然而,导入的方法有点蛮力而且不是很优雅,因为我只有大约 2 周的时间来处理它。
- I have linked tables set up in the database to CSV files
- I have append queries that will add new records to existing tables. Warnings are thrown for duplicate entries, but those can be ignored.
- 我已将数据库中设置的表链接到 CSV 文件
- 我有追加查询,将新记录添加到现有表。重复条目会抛出警告,但可以忽略这些。
What my company wants to do is every day run a program I created to download these reports, on a rolling interval of about 30 days. Then add any new records into the Access database.
我的公司想要做的是每天运行我创建的程序来下载这些报告,滚动间隔大约为 30 天。然后将任何新记录添加到 Access 数据库中。
Since I'm leaving soon, I won't have time to test this database, and would like to have some method of documenting errors and warnings that are thrown; everything from a duplicate entry warning to a type mismatch error, or a syntax error in some SQL query. Is this possible and if so what do you think would be the most effective way to go about it? Maybe while my import macro is running open up an error handling function? We are working in Access 2007 if that helps.
由于我很快就要离开了,我没有时间测试这个数据库,并且想要一些方法来记录抛出的错误和警告;从重复条目警告到类型不匹配错误,或某些 SQL 查询中的语法错误。这可能吗?如果可能,您认为最有效的方法是什么?也许在我的导入宏运行时打开错误处理功能?如果有帮助,我们正在使用 Access 2007。
回答by Fionnuala
You can write to a text file, for the most part, in the error handling routine for each relevant procedure. You may need to watch out for the more serious errors and do something else with them. You will also probably need to watch out for DAO errors, not quite the same thing as code errors (http://office.microsoft.com/en-us/access-help/HV080753531.aspx). There may be other errors that you wish to raise yourself:
大多数情况下,您可以在每个相关过程的错误处理例程中写入文本文件。您可能需要注意更严重的错误并对其进行处理。您可能还需要注意 DAO 错误,这与代码错误不同(http://office.microsoft.com/en-us/access-help/HV080753531.aspx)。您可能希望自己提出其他错误:
Err.Raise vbObjectError + 100
See: http://msdn.microsoft.com/en-us/library/aa241678(v=vs.60).aspx
请参阅:http: //msdn.microsoft.com/en-us/library/aa241678(v=vs.60).aspx
LogError (ErrNo & " " & ErrDescr & " " & ErrInfo)
Sub LogError(strError)
Const ForAppending = 8
Dim strPath As String
Dim fs As Object
Dim a As Object
strPath = GetDataDirectory
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists(strPath & "\ErrorLog.txt") = True Then
Set a = fs.OpenTextFile(strPath & "\ErrorLog.txt", ForAppending)
Else
Set a = fs.createtextfile(strPath & "\ErrorLog.txt")
End If
a.WriteLine Date + Time & " " & strError
a.Close
Set fs = Nothing
End Sub
More info: http://msdn.microsoft.com/en-us/library/bb221208(v=office.12).aspx
更多信息:http: //msdn.microsoft.com/en-us/library/bb221208(v=office.12).aspx