2

我已经完成了一个数据库的创建,它在我的计算机上运行良好。我正在使用 access 2013,在我的 VBA 代码中,我为大多数数据库中使用的每个函数/子编写了错误处理程序。但是,它设计用于具有 Access 运行时 2007 的用户,每次我在他们的机器上运行它时,我都会收到一个未捕获的错误“由于运行时错误,此应用程序的执行已停止”。

命令按钮的代码。

选项比较数据库

 Private Sub Command0_Click()
 Dim ErrorStep As String
 DoCmd.SetWarnings False
 '-------------------------------------------------------------------------------------
 ' Procedure : Command0_Click
 ' Author    : Chris Sparkes
 ' Date      : 13/08/2013
 '-------------------------------------------------------------------------------------
 ErrorStep = "1 - Cleansing Records"
 DoCmd.OpenQuery "qry1_3"
 DoCmd.OpenQuery "qry4-7"
 DoCmd.OpenQuery "qry9"

 Call ExcelOutputReport

 Exit_Command0_Click:
 On Error GoTo 0
 Exit Sub

 Command0_Click_Error:
 MsgBox "Error in procedure Command0_Click of VBA Document."
 GoTo Exit_Command0_Click
 On Error GoTo 0

 End Sub


 Public Function ExcelOutputReport()

Dim ErrorStep As String
DoCmd.SetWarnings False
'---------------------------------------------------------------------------------------
' Procedure : ExcelOutputReport
' Author    : Chris Sparkes
' Date      : 13/08/2013
'---------------------------------------------------------------------------------------
ErrorStep = "1 - Cleansing Records"

 Dim dbLocal As DAO.Database
    Dim tbloutput As DAO.Recordset
    'DAO Declarations

    Dim objExcel As New Excel.Application
    Dim objWorkbook As Excel.Workbook
    Dim objWorksheet As Excel.Worksheet


    Dim IntCurrTask As Integer
    Dim blurb As String

    Set dbLocal = CurrentDb()
    Set tbloutput = dbLocal.OpenRecordset("tbl_output")

    Set objExcel = CreateObject("Excel.Application")
    Set objWorkbook = objExcel.Workbooks.Open("G:\Enliven Sales                 Report\Envliven_Report_Template_1.xls")
    Set objWorksheet = objWorkbook.Worksheets("Enliven")

    objExcel.Visible = True
    objWorkbook.Windows(1).Visible = True

    tbloutput.MoveFirst
    IntCurrTask = 2

Do While Not tbloutput.EOF
    With objWorksheet

    .Cells(IntCurrTask, 1).Value = tbloutput![CustomerOrderCode]
    .Cells(IntCurrTask, 2).Value = tbloutput![CustomerCode]
    .Cells(IntCurrTask, 3).Value = tbloutput![CustomerDescription]
    .Cells(IntCurrTask, 4).Value = tbloutput![ItemCode]
    .Cells(IntCurrTask, 5).Value = tbloutput![ItemDescription]
    .Cells(IntCurrTask, 6).Value = tbloutput![DateOrderPlaced]
    .Cells(IntCurrTask, 7).Value = tbloutput![CustomerDueDate]
    .Cells(IntCurrTask, 8).Value = tbloutput![Quantity]
    .Cells(IntCurrTask, 9).Value = tbloutput![ShippedQuantity]

          End With

        IntCurrTask = IntCurrTask + 1
        tbloutput.MoveNext
    Loop

    tbloutput.Close
    dbLocal.Close

    DoCmd.SetWarnings True

        Set tbloutput = Nothing
        Set dbLocal = Nothing
        Set objWorksheet = Nothing
        Set objWorkbook = Nothing
        Set objExcel = Nothing

Exit_ExcelOutputReport:
    On Error GoTo 0
    Exit Function

ExcelOutputReport_Error:
    MsgBox "Error at in procedure ExcelOutputReport of VBA Document."
    GoTo Exit_ExcelOutputReport

    End Function

有没有人有任何想法可能导致这种情况?引用应该没问题,因为我使用的引用与我知道的与我制作的不同数据库一起使用过的相同。

谢谢,克里斯

4

1 回答 1

1

您添加了错误例程,但没有激活它们。在方法的开头,添加一条On Error Goto语句:

Private Sub Command0_Click()
    On Error Goto Command0_Click_Error
    ...
End Sub

Public Function ExcelOutputReport()
    On Error Goto ExcelOutputReport_Error
    ...
End Sub

在您的错误例程中,您应该(至少)显示内容Err.Description而不是通用错误消息。否则,您将很难跟踪错误的来源。例如:

MsgBox "Error in procedure Command0_Click: " & Err.Description
于 2013-08-13T13:04:48.413 回答