【问题标题】:VBA How to exit function on error? Not working. Access 2007VBA如何在错误时退出函数?不工作。访问 2007
【发布时间】:2015-08-04 17:56:17
【问题描述】:

在 Access 2007 中,我的错误捕获设置为 Break on Unhandled Errors

我希望代码在发生错误的行停止并退出函数,而不是恢复到下一行代码。但是,它似乎对我不起作用。我故意在第 6 行创建了一个错误,以查看它是否会在此行之后退出函数,但它只提示错误处理程序消息,并在发生错误后继续恢复到下一行。 这是我的代码:

GoToBackend():

'Go to current linked backend database

Private Function GoToBackend()

On Error GoTo BackendErrorHandler

    'To update BEPath requires two sets of proc.
    'Delete Exisiting
     RunQuery "DeleteBEEPath"   'Here is where I created error by miss spelling it
    'Insert Into
     RunQuery "InsertBEPath"

    'Prompt alert
    MsgBox "Front end tables succesfully linked. Access now needs to run the backend database to complete the linking process. Please ensure macros/vba are enabled if prompted.", 48
    Hyperlink.GoHyperlink (Hyperlink.PrepHyperlink(GetBackendPath))

ExitFunction:
    Exit Function   'Why won't this exit the function? 

BackendErrorHandler:
    Dim Msg As String
    Msg = Err.Number & ": " & Err.Description
    MsgBox Msg
    Resume ExitFunction
End Function     

RunQuery():

'Run a given query name
Private Function RunQuery(qName As String)
On Error GoTo RunQueryErrorHandler
   DoCmd.SetWarnings False
   DoCmd.OpenQuery qName
   DoCmd.SetWarnings True

ExitFunction:
    Exit Function
RunQueryErrorHandler:
    Dim Msg As String
    Msg = Err.Number & ": " & Err.Description
    MsgBox Msg
    Resume ExitFunction
End Function 

【问题讨论】:

    标签: ms-access error-handling vba ms-access-2007


    【解决方案1】:

    错误本身发生在您的RunQuery 函数中,因此错误在此处得到处理。在那里你说用Msg = Err.Number & ": " & Err.Description显示错误消息,因此它会弹出,错误被认为是“已处理”,原始函数继续运行。

    你的台词:

    RunQuery "DeleteBEEPath"

    不在乎你的字符串是什么,在它看来你已经正确地为它提供了一个字符串来传递给函数。一旦它进入实际错误发生的函数。我没有对此进行测试,但我相信如果您关闭第二个函数中的错误处理,那么GoToBackend 中的错误处理应该以您希望的方式处理它。所以你的第二个函数是这样的:

    Private Function RunQuery(qName As String)
       DoCmd.SetWarnings False
       DoCmd.OpenQuery qName
       DoCmd.SetWarnings True
    End Function 
    

    再一次,我真的无法对此进行测试,但这应该能让你走上正确的道路,或者如果周围有更多经验的人,他们可以提供比我更好的答案。

    我还建议将DoCmd.SetWarnings (True) 放入您的错误处理中,这样如果在您将其设置为 false 后发生错误,它们将被重新打开。

    编辑:我想我会加入我新概念化的功能。 (未经测试)

    Public Function RunSQLNoWarnings(strSQLQuery As String) As Boolean
    On Error GoTo Err_Handler
    
    
        DoCmd.SetWarnings (False)
        DoCmd.RunSQL (strSQLQuery)
        DoCmd.SetWarnings (True)
    
        RunSQLNoWarnings = True
    
    Exit_Handler:
        Exit Function
    
    Err_Handler:
        DoCmd.SetWarnings (True)
        Call LogError(Err.Number, Err.Description, strMODULE_NAME & ".RunSQLNoWarnings on SQL Query: " & strSQLQuery)
        RunSQLNoWarnings = False
        Resume Exit_Handler
    End Function
    

    【讨论】:

    • 啊,我明白了。这工作谢谢。我想我不能只对每个过程进行通用错误处理,或者假设首先知道可能会发生什么错误。
    • 哈哈,我和你运行 SQL 的功能完全一样,我现在必须检查我的数据库中的所有地方,看看是否有任何情况需要我担心会发生同样的事情。我最初的想法是,我必须将其更改为布尔返回函数,以便可以用来确定使用它的函数接下来应该发生什么。
    • 我添加了我的功能将要更新的内容。可能对你也有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2023-01-27
    • 1970-01-01
    • 2013-07-20
    • 2016-08-23
    相关资源
    最近更新 更多