【问题标题】:DCount not finding matching records in AccessDCount 在 Access 中找不到匹配的记录
【发布时间】:2016-10-08 08:27:38
【问题描述】:

我没有得到正确的 DCount 值

表是“课程”:
ID(自动)pk
InstructorID 为整数
作为日期的星期
StudentID 为整数
状态为字节

我的功能是

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我在一些示例中看到了 # 分隔符,但找不到任何文档。应该追的。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 2011-11-25
    • 2017-06-25
    相关资源
    最近更新 更多