计数功能

Dcount function

我想统计调用次数,Datecall[Username]是table的字段名BCKHDY但是为什么numbercall总是等于0 . 如果我删除 AND DateCall= #" & DateFrom & "#,代码 运行,那意味着 Datecall 有问题。怎么了?

Private Sub txtnbCall_Click()
    Dim mydept As Integer

DateFrom = Me.txtfrom.Value
User = Forms![Navigation form]![txtLogin].Value
    If Not IsNull(DLookup("Deptname", "tblUser", "UserLogin = '" & User & "'")) Then
    mydept = DLookup("Deptname", "tblUser", "UserLogin = '" & User & "'")
    Me.txtnbCall = numbercall(mydept, DateFrom)
    End If
End Sub


Public Function numbercall(ByVal mydept As Integer, _
                            ByVal DateFrom As Date) As Integer
    numbercall = DCount("CompanyName", "BCKHDY", _
                  "[UserName] = " & mydept & "AND DateCall >= #" & DateFrom & "#")
End Function

你在这里漏了 space:

mydept & "AND 

应该是

mydept & " AND

只有 space 个字符串在计数。如果您忘记了 space,则条件将包括类似 1And

的内容

您还需要将日期格式化为 yyyy-MM-dd 或 MM/dd/yyyy:

"[UserName] = " & mydept & " AND DateCall >= #" & Format(DateFrom, "yyyy-MM-dd") & "#")

您不必两次调用 DLookup,声明所有变量,并且您可能在 DCount 中过滤了错误的字段:

Private Sub txtnbCall_Click()

    Dim mydept    As Variant
    Dim DateFrom  As Date
    Dim User      As String

    DateFrom = Me!txtfrom.Value
    User = Forms![Navigation form]![txtLogin].Value

    mydept = DLookup("Deptname", "tblUser", "UserLogin = '" & User & "'")       
    If Not IsNull(mydept) Then
        Me!txtnbCall.Value = numbercall(mydept, DateFrom)
    End If

End Sub


Public Function numbercall(ByVal mydept As Integer, _
                            ByVal DateFrom As Date) As Integer

    numbercall = DCount("*", "BCKHDY", _
                  "[Deptname] = " & mydept & " AND DateCall >= #" & Format(DateFrom, "yyyy\/mm\/dd") & "#")

End Function