DLookup多条准则运行时错误

时间:2013-11-27 05:05:38

标签: ms-access access-vba runtime-error

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

我目前收到此错误:

运行时错误'3075': 查询表达式'AccountID =&中的语法错误(缺少运算符)表格![frmInvoices]!AccountID& Billing_Month =&表格![frmInvoices]!Billing_Month& Billing_Year =&窗体![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

1 个答案:

答案 0 :(得分:0)

使第三个DLookup参数成为一个字符串,其中包含Forms![frmInvoices]个控件引用。 (db引擎在评估表达式时可以取消引用这些控件。)

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 构建的内容。