首页  ·  知识 ·  编程语言
VB错误处理中心过程,写数据库日志表或写日志文件
网友  http://blog.csdn.net/fangke/  VB  编辑:德仔   图片来源:网络
Public Sub ShowError(strModule As String, strProcedure As String, lngErrorNumber As Long, strErrorDescription As String, showMsg As String
Public Sub ShowError(strModule As String, strProcedure As String, lngErrorNumber As Long, strErrorDescription As String, showMsg As String)
    '
    '错误处理中心过程,写数据库日志表或写日志文件
    '
    'strModule           '模块名称
    'strProcedure        '过程名称
    'lngErrorNumber      '错误ID号
    'strErrorDescription '错误描述
    'showMsg             '是否显示本过程内错误显示信息(值:"Y" or "N")
   
    'Error表结构(f001 (Date)发生时间,    f002 (nvarchar50)模块名称, f003 (nvarchar50)过程名称, f004 (nvarchar50)错误ID号, _
                 f005 (nvarchar300)错误描述,f006 (nvarchar50)版 本 号, f007 (nvarchar50)用户名称, f008 (nvarchar50)网卡地址
    'ErrorCode表结构 f001 (nvarchar20)错误代码,  f002 (nvarchar255)错误信息, f003 (numeric9)错误级别
    '         级别说明: '10'以下,一般错误,不影响操作
    '                   '11-20',严重错误,不能操作,程序执行退出
   
    On Error GoTo ErrorHandle
    Dim strMessage As String
    Dim strCaption As String
    Dim sVer As String
    Dim intLogFile As Integer
    Dim Res As New ADODB.Recordset
    Dim ResErrorCode As New ADODB.Recordset
    Dim strSQL As String
   
    '对应错误号,从ErrorCode表中找到对应的错误信息,0-1000 错误号保留给VB
    DBOpen ResErrorCode, "select * from errorcode where f001='" & lngErrorNumber & "'"
    If Not (ResErrorCode.EOF Or ResErrorCode.BOF) Then
        strMessage = ResErrorCode.Fields("f002")
        If ResErrorCode.Fields("f003") > 10 Then
            MsgBox "产生一个严重错误,可能影响到系统的可操作性,请立即联系本系统开发人员!", vbCritical, "严重错误"
        End If
    End If
         
    '写错误入文件----------------------------
   
    intLogFile = FreeFile
    Open App.Path & "\" & strIni.LogFile For Append As #intLogFile
    Print #intLogFile, "***错误"; VBA.Now & "*** " & "Version:" & _
          str$(App.Major) & "." & str$(App.Minor) & "." & Format(App.Revision, "0000")
    Print #intLogFile, "Error: " & lngErrorNumber
    Print #intLogFile, "Description: " & strErrorDescription
    Print #intLogFile, "Module: " & strModule
    Print #intLogFile, "Procedure: " & strProcedure
    Print #intLogFile, ""
    Close #intLogFile
   
    If Len(strMessage) > 2 Then strErrorDescription = strMessage
   
    strMessage = "错误: " & "(" & lngErrorNumber & ")" & strErrorDescription & vbCrLf & vbCrLf & _
                 "模块:" & strModule & ";  过程:" & strProcedure
    sVer = Trim(str$(App.Major) & "." & str$(App.Minor) & "." & _
                 Format(App.Revision, "0000"))
    strCaption = "错误 Version: " & sVer
                          
    '写错误入数据库表--------------------------
    strSQL = "insert into error(f001,f002,f003,f004,f005,f006,f007,f008) values(" _
                & DateFmtB & VBA.Now & DateFmtE & "," _
                & IIf(Len(Trim(strModule)) = 0, "null", "'" & strModule & "'") & "," _
                & IIf(Len(Trim(strProcedure)) = 0, "null", "'" & strProcedure & "'") & "," _
                & IIf(Len(Trim(lngErrorNumber)) = 0, "null", "'" & lngErrorNumber & "'") & "," _
                & IIf(Len(Trim(strErrorDescription)) = 0, "null", "'" & Replace(strErrorDescription, "'", "") & "'") & "," _
                & IIf(Len(Trim(sVer)) = 0, "null", "'" & sVer & "'") & "," _
                & IIf(Len(Trim(sUserName)) = 0, "null", "'" & sUserName & "'") & "," _
                & IIf(Len(Trim(sVer)) = 0, "null", "'" & EthernetNO & "'") & ")"
         
  
    Cn.Execute strSQL
        
    '是否显示未知错误信息
    If Trim(UCase(showMsg)) = "Y" Then MsgBox strMessage, vbCritical, strCaption
   
PROC_EXIT:
    Set Res = Nothing
    Set ResErrorCode = Nothing
    Exit Sub
ErrorHandle:
    Resume Next
End Sub
 
 
本文作者:网友 来源:网络http://blog.csdn.net/fangke/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读