首页 > 解决方案 > 访问 DLookUp 返回 Null

问题描述

我一直在寻找,似乎无法找到答案。我遵循了在网上找到的其他示例代码的示例,但没有任何效果。我试图从我的数据库查询中的特定单元格中获取一串评论到同一数据库中表单的文本框中。我收到错误 13 不匹配错误和 96?空错误。下面是我的代码:

Private Sub Text757_Click()

    Dim Remarks757 As String

    If IsNull(DLookup("Remarks", "QueryDataPaveAll", "[YrRated] = Forms![FormDataEntryPave]![TextYrRated]" And "[RdSecNo] = Forms![FormDataEntryPave]![TextRdSecNo]")) Then
        MsgBox "No Record Found"

    Else
        Remarks757 = DLookup("Remarks", "QueryDataPaveAll", "[YrRated] = Forms![FormDataEntryPave]![TextYrRated]" And "[RdSecNo] = Forms![FormDataEntryPave]![TextRdSecNo]")
    Me.Text757 = Remarks757
    End If

End Sub

标签: vbams-access

解决方案


问题是Nullfrom 找不到任何东西时的返回值不能存储为 a String,因此您必须声明Remarks757为 aVariant

Private Sub Text757_Click()

    Dim Remarks757 As Variant
    Remarks757 = DLookup("Remarks", "QueryDataPaveAll", "[YrRated] = Forms![FormDataEntryPave]![TextYrRated]" And "[RdSecNo] = Forms![FormDataEntryPave]![TextRdSecNo]")

    If IsNull(Remarks757) Then
        MsgBox "No Record Found"
    Else
        Me.Text757 = Remarks757
    End If

End Sub

推荐阅读