【问题标题】:DLookup Multiple Criteria Runtime ErrorDLookup 多条件运行时错误
【发布时间】:2013-11-27 05:05:37
【问题描述】:

我正在尝试在 Access 2010 中创建具有多个条件的 DLookup,但遇到了一些麻烦。我通过表格创建发票。在发票表单上,我选择 AccountID,并设置计费月份和年份。根据这些信息,我想搜索我的预付款查询 (quePrepayment) 并提取符合这三个条件的任何预付款。

我目前收到此错误:

运行时错误“3075”: 查询表达式“AccountID= & Forms![frmInvoices]!AccountID & Billing_Month = & Forms![frmInvoices]!Billing_Month & Billing_Year = & Forms![frmInvoices]!Billing_Year)”中的语法错误(缺少运算符)

Private Sub AccountID_Change()
Billing_Prepayment = DLookup("Total_Prepayment", "quePrepayment", "[AccountID] = & Forms![frmInvoices]!AccountID And [Billing_Month] = & Forms![frmInvoices]!Billing_Month And [Billing_Year] = & Forms![frmInvoices]!Billing_Year")
End Sub

【问题讨论】:

    标签: ms-access vba runtime-error


    【解决方案1】:

    将第三个DLookup 参数设为一个字符串,其中内置了Forms![frmInvoices] 控件引用。 (数据库引擎在计算表达式时可以取消引用这些控件。)

    Billing_Prepayment = DLookup("Total_Prepayment", "quePrepayment", _
        "[AccountID] = Forms![frmInvoices]!AccountID And [Billing_Month] = Forms![frmInvoices]!Billing_Month And [Billing_Year] = Forms![frmInvoices]!Billing_Year")
    

    但是,该字符串太长了,很难确定它是否正确构建。您可以改用这样的方法...

    Dim strCriteria As String
    strCriteria = "[AccountID] = Forms![frmInvoices]!AccountID " & _
        "And [Billing_Month] = Forms![frmInvoices]!Billing_Month " & _
        "And [Billing_Year] = Forms![frmInvoices]!Billing_Year")
    Debug.Print strCriteria
    Billing_Prepayment = DLookup("Total_Prepayment", "quePrepayment", _
        strCriteria)
    

    如果遇到问题,您可以转到立即窗口 (Ctrl+g) 来检查为 strCriteria 构建的内容。

    【讨论】:

    • 感谢您的信息。我能够让它工作,看看我做错了什么。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 2020-09-11
    相关资源
    最近更新 更多