错误“条件表达式中的数据类型不匹配”

Posted

技术标签:

【中文标题】错误“条件表达式中的数据类型不匹配”【英文标题】:Error "Data type mismatch in criteria expression" 【发布时间】:2014-07-24 16:23:37 【问题描述】:

此代码有,我认为这是因为我的查询 QryStockRinci 中的 strDate 和数据。

公共函数 ReturnAmountSaleRev(strDate As Date, strProductID As String, curAmount As Currency, curAmountSale As Currency) As Variant

Dim curAmountSaleUpToCurrentPO As Integer Dim varAmountSalePriorToCurrentPO As Variant 'Get the total Amount for the current ProductID up to and including given PO. curAmountSaleUpToCurrentPO = DSum("Stock", "QryStockRinci", "[Tanggal] <= '" & strDate & "' AND [kode_barcode] = '" & strProductID & "'") 'If there is enough in SalesAmount to cover the whole cost, return the whole Amount. If curAmountSale - curAmountSaleUpToCurrentPO >= 0 Then ReturnAmountSaleRev = Format(curAmount, "0.00") Else 'Get the the total Amount in ProductID prior to current PO. varAmountSalePriorToCurrentPO = DSum("Stock", "QryStockRinci", "[Tanggal] < '" & strDate & "' AND [kode_barcode] = '" & strProductID & "'") 'If current PO is first in ProductID, varAmountSalePriorToCurrentPO will be null; 'determine covered amount. If IsNull(varAmountSalePriorToCurrentPO) = True Then If curAmount <= curAmountSale Then ReturnAmountSaleRev = Format(curAmount, "0.00") Else ReturnAmountSaleRev = Format(curAmountSale, "0.00") End If Else 'If current PO is not first in ProductID, varAmountSalePriorToCurrentPO 'will have a value; determine the covered amount. varAmountSalePriorToCurrentPO = curAmountSale - varAmountSalePriorToCurrentPO If varAmountSalePriorToCurrentPO <= 0 Then ReturnAmountSaleRev = 0 Else ReturnAmountSaleRev = Format(varAmountSalePriorToCurrentPO, "0.00") End If End If End If

结束函数

【问题讨论】:

能否告诉我们错误出现在哪一行? curAmountSaleUpToCurrentPO = DSum("Stock", "QryStockRinci", "[Tanggal] 【参考方案1】:

您需要将日期视为日期而不是字符串,还需要尝试用 Nz 括起域函数以避免将 Null 值分配给 Integer 或除 Variant 之外的任何其他数据类型。

Public Function ReturnAmountSaleRev(strDate As Date, strProductID As String, curAmount As Currency, curAmountSale As Currency) As Variant
    Dim curAmountSaleUpToCurrentPO As Long
    Dim varAmountSalePriorToCurrentPO As Variant

    'Get the total Amount for the current ProductID up to and including given PO.'
    curAmountSaleUpToCurrentPO = Nz(DSum("Stock", "QryStockRinci", "[Tanggal] <= " & Format(strDate, "\#mm\/dd\/yyyy\#") & " AND [kode_barcode] = '" & strProductID & "'"), 0)

    'If there is enough in SalesAmount to cover the whole cost, return the whole Amount.'
    If curAmountSale - curAmountSaleUpToCurrentPO >= 0 Then
        ReturnAmountSaleRev = Format(curAmount, "0.00")
    Else
        'Get the the total Amount in ProductID prior to current PO.'
        varAmountSalePriorToCurrentPO = Nz(DSum("Stock", "QryStockRinci", "[Tanggal] < " & Format(strDate, "\#mm\/dd\/yyyy\#") & "' AND [kode_barcode] = '" & strProductID & "'"), 0)

        'If current PO is first in ProductID, varAmountSalePriorToCurrentPO will be null;'
        'determine covered amount.'
        If IsNull(varAmountSalePriorToCurrentPO) = True Then
            If curAmount <= curAmountSale Then
                ReturnAmountSaleRev = Format(curAmount, "0.00")
            Else
                ReturnAmountSaleRev = Format(curAmountSale, "0.00")
            End If
        Else
            'If current PO is not first in ProductID, varAmountSalePriorToCurrentPO
            'will have a value; determine the covered amount.'
            varAmountSalePriorToCurrentPO = curAmountSale - varAmountSalePriorToCurrentPO

            If varAmountSalePriorToCurrentPO <= 0 Then
                ReturnAmountSaleRev = 0
            Else
                ReturnAmountSaleRev = Format(varAmountSalePriorToCurrentPO, "0.00")
            End If
        End If
    End If
End Function

【讨论】:

感谢 Paul,但它返回指向同一语句的溢出错误 您的值必须是大于整数可以处理的数字,尝试将它们声明为 Long。另外请始终参考引发错误的代码行。否则很难识别。 保罗,我对这个功能有点问题。如果同一产品有相同日期的数据,则返回错误值。也许我需要添加一个字段 row_number 来区分数据。你能帮帮我吗? 当然,如果您提供一些示例数据可能会有所帮助。 我已经解决了这个问题,为该表添加了自动编号字段,并按照我想要的顺序将数据插入到该表排序

以上是关于错误“条件表达式中的数据类型不匹配”的主要内容,如果未能解决你的问题,请参考以下文章

条件表达式中的数据类型不匹配

ms 访问 2007 更新错误运行时错误 3464,条件表达式中的数据类型不匹配

错误:条件表达式中的数据类型不匹配

尝试插入 Access 数据库时,条件表达式中的数据类型不匹配错误

试图跟踪条件表达式中的数据类型不匹配

条件表达式中的数据类型不匹配 (Access DB)