2016-10-08 86 views
1

,我没有得到正确的价值DCOUNTDCOUNT在Access

表是 “教训” 没有找到符合条件的记录:
ID(自动)PK
InstructorID作为整数
Weekof截止日期
StudentID作为整数
地位字节

3 records

我的F油膏是

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表达式中的错误吗?

回答

2

当构建您需要界定与哈希标记(#)的日期值条件,为字符串参数。此外,为了安全起见,应该被格式化为一个明确的yyyy-mm-dd日期字符串:

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

我看到了在一些例子中,#分隔符,但无法找到它的任何文档。应该追求。非常感谢。 – user2978241