Search code examples
ms-accessvbams-access-2010dcount

Dcount function


I want to count the call number, Datecall, [Username] is the name of fields of table BCKHDY but why numbercall always equal 0. If I delete AND DateCall= #" & DateFrom & "#, code run, that mean there is something wrong with Datecall. What 's wrong?

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

Solution

  • You're missing a space here:

    mydept & "AND 
    

    Should be

    mydept & " AND
    

    Only spaces inside a string count. If you forget the space, the criteria would include something like 1And

    You also need to format the date as either yyyy-MM-dd or MM/dd/yyyy:

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