对。所以我决定,当我打算拆分数据库的FE / BE时,我想使用'Allen Brown's Error Handling in VBA',因为这将使进程停止,通知用户该操作失败并自动为该错误记录日志我稍后再审核。
唯一的问题是我不断收到错误消息“预期变量过程而不是模块”
现在,我确实略微修改了Allen的代码,所以我不再将其称为“ LogError”,而是将其全部更改为“ LogAutoErrors”
这是我添加了引发上述错误的调用代码的第一个子项
Private Sub ImportAttendees_Click()
On Error GoTo ImportAttendees_Click_Err
Dim SelectedFile As String
Dim FilePicker As FileDialog
Dim SQLdelete As String
Set FilePicker = Application.FileDialog(msoFileDialogFilePicker)
FilePicker.AllowMultiSelect = False
FilePicker.Filters.Add "Excel", "*.xls*", 1
FilePicker.InitialFileName = "C:\Users\"
FilePicker.Title = "Select New Attendee List Location..."
FilePicker.Show
If FilePicker.SelectedItems.Count <> 0 Then
SelectedFile = FilePicker.SelectedItems(1)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tbl_STG_AttendeeImport", SelectedFile, True
MsgBox prompt:="Data Staged - Ready For Import", buttons:=vbInformation, Title:="Data Loaded"
End If
Me.Refresh
Exit Sub
ErrorHandler:
ImportAttendees_Click_Err: ' Label to jump to on error.
MsgBox prompt:="E-Link encountered an error when processing the last action. E-Link has cancelled the last action and the error has been logged with the system administrator", buttons:=vbInformation, Title:="Database Process Error"
Call logAutoErrors(Err.Number, Err.Description, "ImportAttendees_Click()")
Resume Exit_ImportAttendees_Click
End Select
End Sub
期望的结果是,每当我遇到错误(例如,我最喜欢的运行时错误3061)时,它就会插入错误表并取消操作。
编辑:这是修改后的艾伦·布朗(Allen Browne)代码,我唯一更改的是一个字段名称和模块名称
Function logAutoErrors(ByVal lngErrNumber As Long, ByVal strErrDescription As String, _
strCallingProc As String, Optional vParameters, Optional bShowUser As Boolean = False) As Boolean
On Error GoTo Err_logAutoErrors
' Purpose: Generic error handler.
' Logs errors to table "tLogError".
' Arguments: lngErrNumber - value of Err.Number
' strErrDescription - value of Err.Description
' strCallingProc - name of sub|function that generated the error.
' vParameters - optional string: List of parameters to record.
' bShowUser - optional boolean: If False, suppresses display.
' Author: Allen Browne, allen@allenbrowne.com
Dim strMsg As String ' String for display in MsgBox
Dim rst As DAO.Recordset ' The tLogError table
Select Case lngErrNumber
Case 0
Debug.Print strCallingProc & " called error 0."
Case 2501 ' Cancelled
'Do nothing.
Case 3314, 2101, 2115 ' Can't save.
If bShowUser Then
strMsg = "Record cannot be saved at this time." & vbCrLf & _
"Complete the entry, or press <Esc> to undo."
MsgBox strMsg, vbExclamation, strCallingProc
End If
Case Else
If bShowUser Then
strMsg = "Error " & lngErrNumber & ": " & strErrDescription
MsgBox strMsg, vbExclamation, strCallingProc
End If
Set rst = CurrentDb.OpenRecordset("tbl_ADM_ErrorLog", , dbAppendOnly)
rst.AddNew
rst![ErrNumber] = lngErrNumber
rst![ErrDescription] = Left$(strErrDescription, 255)
rst![ErrDate] = Now()
rst![CallingProc] = strCallingProc
rst![UserID] = TempVars!AUID
If Not IsMissing(vParameters) Then
rst![Parameters] = Left(vParameters, 255)
End If
rst.Update
rst.Close
LogError = True
End Select
Exit_logAutoErrors:
Set rst = Nothing
Exit Function
Err_logAutoErrors:
strMsg = "An unexpected situation arose in your program." & vbCrLf & _
"Please write down the following details:" & vbCrLf & vbCrLf & _
"Calling Proc: " & strCallingProc & vbCrLf & _
"Error Number " & lngErrNumber & vbCrLf & strErrDescription & vbCrLf & vbCrLf & _
"Unable to record because Error " & Err.Number & vbCrLf & Err.Description
MsgBox strMsg, vbCritical, "logAutoErrors()"
Resume Exit_logAutoErrors
End Function
答案 0 :(得分:1)
缺少标签, 您应该将其放置在正确的位置
的标签
Resume Exit_ImportAttendees_Click
;
Private Sub ImportAttendees_Click()
On Error GoTo ImportAttendees_Click_Err
Dim SelectedFile As String
Dim FilePicker As FileDialog
Dim SQLdelete As String
Set FilePicker = Application.FileDialog(msoFileDialogFilePicker)
FilePicker.AllowMultiSelect = False
FilePicker.Filters.Add "Excel", "*.xls*", 1
FilePicker.InitialFileName = "C:\Users\"
FilePicker.Title = "Select New Attendee List Location..."
FilePicker.Show
If FilePicker.SelectedItems.Count <> 0 Then
SelectedFile = FilePicker.SelectedItems(1)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tbl_STG_AttendeeImport", SelectedFile, True
MsgBox prompt:="Data Staged - Ready For Import", buttons:=vbInformation, Title:="Data Loaded"
End If
Exit_ImportAttendees_Click:
Me.Refresh
Exit Sub
ErrorHandler:
ImportAttendees_Click_Err: ' Label to jump to on error.
MsgBox prompt:="E-Link encountered an error when processing the last action. E-Link has cancelled the last action and the error has been logged with the system administrator", buttons:=vbInformation, Title:="Database Process Error"
Call logAutoErrors(Err.Number, Err.Description, "ImportAttendees_Click()")
Resume Exit_ImportAttendees_Click
End Select
End Sub
答案 1 :(得分:1)
如果您具有与模块共享相同名称的“功能”或“子”,则可能会发生这种情况。每个名称都必须唯一。甚至模块。
您的线路标签上发生了一些奇怪的事情。您还有一个End Select
,没有select语句。
尝试一下:
Private Sub ImportAttendees_Click()
On Error GoTo ErrorHandler
Dim SelectedFile As String
Dim FilePicker As FileDialog
Dim SQLdelete As String
Set FilePicker = Application.FileDialog(msoFileDialogFilePicker)
With FilePicker
.AllowMultiSelect = False
.Filters.Add "Excel", "*.xls*", 1
.InitialFileName = "C:\Users\"
.Title = "Select New Attendee List Location..."
.Show
End With
If FilePicker.SelectedItems.Count <> 0 Then
SelectedFile = FilePicker.SelectedItems(1)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tbl_STG_AttendeeImport", SelectedFile, True
MsgBox prompt:="Data Staged - Ready For Import", Buttons:=vbInformation, Title:="Data Loaded"
End If
Me.Refresh
Exit Sub
ErrorHandler:
MsgBox prompt:="E-Link encountered an error when processing the last action. E-Link has cancelled the last action and the error has been logged with the system administrator", Buttons:=vbInformation, Title:="Database Process Error"
logAutoErrors Err.Number, Err.Description, "ImportAttendees_Click()"
Resume
End Sub