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

时间:2016-10-08 08:27:38

标签: ms-access access-vba ms-access-2010 dcount

我没有为DCount

获取正确的值

表是"课程":
    ID(自动)pk
    InstructorID作为整数
    周日作为日期
    StudentID作为整数
    状态为字节

3 records

我的功能是

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

该函数从即时窗口调用:

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

以下select语句确实返回正确的记录数(应为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表达式中的错误吗?

1 个答案:

答案 0 :(得分:2)

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

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