DCount 在 Access 中找不到匹配的记录

DCount not finding matching records in Access

我没有得到 DCount 的正确值

Table 是 "Lessons":
ID(自动)pk
InstructorID 为整数
截止日期
学生 ID 为整数
字节状态

我的职能是

Public Function NoRecordsFound(ByVal instructorid As Integer, ByVal weekof As Date, studentid As Integer) As Integer

    Dim strCriteria As String
    strCriteria = "Lessons.[InstructorID] = " & instructorid & " AND Lessons.[WeekOf] = " & weekof & " AND Lessons.[StudentID] = " & studentid

    NoRecordsFound = DCount("*", "Lessons", strCriteria)
End Function

该函数从立即数 window 调用为:

 :Debug.Print(NoRecordsFound(5, DateValue("10/3/2016"), 17043))

以下 select 语句 return 正确的记录数(应为 1):

SELECT Lessons.[ID], Lessons.[InstructorID], Lessons.[WeekOf], Lessons.[StudentID], Lessons.[Status]
FROM Lessons
WHERE (((Lessons.[InstructorID])=5) AND ((Lessons.[WeekOf])=DateValue("10/3/2016")) AND ((Lessons.[StudentID])=17043));

有人可以帮助找出我的 DCount 表达式中的错误吗?

在为条件构建字符串参数时,您需要使用井号 (#) 分隔日期值。此外,为了安全起见,它应该被格式化为明确的 yyyy-mm-dd 日期字符串:

strCriteria = "Lessons.[InstructorID] = " & instructorid & " " & _
        "AND Lessons.[WeekOf] = #" & Format(weekof, "yyyy-mm-dd") & "# " & _
        "AND Lessons.[StudentID] = " & studentid