VBA - 调用模块导致“预期变量过程而非模块”

问题描述 投票:0回答:2

对。所以我决定当我转向拆分我的数据库的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, [email protected]

    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
vba ms-access access-vba
2个回答
1
投票

如果您有一个与模块共享同一名称的Function或Sub,则会发生这种情况。每个名字都必须是唯一的。甚至模块。

示例:enter image description here注意左侧的模块。这就是你的问题所在。

你的线标签发生了一些奇怪的事情。你也有一个没有选择声明的End 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

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
© www.soinside.com 2019 - 2024. All rights reserved.