DCount返回0但应返回1

时间:2015-05-25 20:00:29

标签: vba ms-access access-vba

我的VBA中有以下DCount代码:

If DCount("*", "tbl2Employee_Order", _
    "[Operation_Date] = #" & Operation_Date & _
    "# AND [Employee_ID]= " & Employee_ID & _
    " AND [Order_ID] = " & Order_ID & _
    " AND [Model_Operation_ID] = " & MO_ID) = 0 Then

    'some code to insert into tbl2Employee_Order
Else
    'some code to update the existing record
End If

但是,即使记录已存在,我的DCount也始终返回0。以下内容:

Debug.Print Operation_Date, Employee_ID, Order_ID, MO_ID, DCount("*", "tbl2Employee_Order", "[Operation_Date] = #" & Operation_Date & "# AND [Employee_ID]= " & Employee_ID & " AND [Order_ID] = " & Order_ID & " AND [Model_Operation_ID] = " & MO_ID)

返回预期值,例如:

08/05/2015     2             526           1107          0 

除了最后一个,预计为1(记录已经存在)。

tbl2EmployeeOrder有此记录:

Operation_Date: 08/05/2015
Employee_ID: 2
Order_ID: 526
Model_Operation_ID = 1107

有趣的是,它在几周前曾经没有问题地工作过,突然它的行为似乎记录尚未存在。

编辑:

以下内容:

Debug.Print TypeName(Operation_Date), TypeName(Employee_ID), TypeName(Order_ID), TypeName(MO_ID)

结果:

Date          Integer       Integer       Integer

这些也是tbl2Employee_Order中的变量类型。

同样,如果我使用DLookup(带有一些列名)而不是DCount,我会返回Null值。

1 个答案:

答案 0 :(得分:2)

您是否更改了区域设置?怎么样

If DCount("*", "tbl2Employee_Order", _
"[Operation_Date] = #" & Format(Operation_Date,"mm/dd/yyyy") & _
"# AND [Employee_ID]= " & Employee_ID & _
" AND [Order_ID] = " & Order_ID & _
" AND [Model_Operation_ID] = " & MO_ID) = 0 Then

删除日期条件以查看其是否有效