【问题标题】:In VBA, how to handle same error differently from different variables?在 VBA 中,如何处理不同变量的相同错误?
【发布时间】:2017-12-07 00:49:22
【问题描述】:

我在 After_Update 的 Access 中有一个 VBA 函数,用于具有两个变量的组合框。第一个来自组合框,第二个是基于第一个的查询结果。我创建了一些错误处理来处理 Null 值,但我想根据哪个值为 Null 来不同地处理错误。有时用户会在组合框中输入一个名称,然后将其删除,这使得变量为 Null。如果 childID 为 Null 这不是问题,我希望函数简单地退出。如果 visitID 为 Null,我希望它显示错误消息框。我想出的结果显示来自两个变量的 Null 值的错误。

On Error GoTo Error_Handle

childID = Me.cmbChildSearchFirst.Column(0)
visitid = DLookup("Visit.[VisitID]", "Q_VisitID_from_ChildID", "[ChildID] =" & childID)

Error_Handle:
If Err.Number = 94 Then
    MsgBox ("Child is not associated with any visit")
    Exit Sub
End If

【问题讨论】:

    标签: ms-access error-handling vba


    【解决方案1】:

    基本大纲:

    childID = Me.cmbChildSearchFirst.Column(0)
    
    'proceed only if not null.
    If Not IsNull(childID) Then
        If Not IsNull(DLookup("Visit.[VisitID]", "Q_VisitID_from_ChildID", "[ChildID] =" & childID)) Then
            ....
        Else
            MsgBox ("Child is not associated with any visit")
            Exit Sub
        End If
    End If
    

    【讨论】:

    • 这让我得到了答案。我必须包含“On Error Resume Next”并将第一个 if 语句更改为“If IsNull(childID) Then Exit Sub Else...”
    • 如果有帮助,请支持此答案。肿瘤坏死因子。 meta.stackexchange.com/questions/5234/…
    猜你喜欢
    • 1970-01-01
    • 2020-10-20
    • 2017-01-16
    • 2012-05-11
    • 1970-01-01
    • 2016-02-25
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多