Search code examples
ms-accessvbams-access-2007

Filtering a report query


Still a beginning access programmer here. Trying to get the report to work. Here is what I am doing

I first created a report using the Report Wizard using the following query as input

SELECT EmployeeId, Project, StartDate
FROM Tasks;

I have a form wherein I select the employee-id. I want to filter the report based on the employee id selected. Here is what I have for invoking the report

DoCmd.OpenReport "rptEmpWork", acViewPreview, "qryEmpReport", "EmployeeId = " & strempid

qryEmpReport is the name of the query that holds the report query that I mentioned above. The strempid holds the value that was selected in the form. However when I get around to execute this, it prompts me to enter the employee id again. Any ideas as to why I am getting this? I have validated to make sure that the strempid does contain the value selected earlier.


Solution

  • I'll guess Tasks.EmployeeId is text datatype. If my guess is correct, add quotes around the value you supply for EmployeeId:

    DoCmd.OpenReport "rptEmpWork", acViewPreview, "qryEmpReport", "EmployeeId = '" & strempid & "'"
    

    Based on our trouble-shooting exercise in the comments, I think you should give yourself an opportunity to examine the actual string value you're giving to OpenReport for its WhereCondition argument. (It's better to view the actual string instead of trying to imagine what it looks like.)

    Dim strWhereCondition As String
    strWhereCondition = "EmployeeId = '" & strempid & "'"
    Debug.Print "strWhereCondition ->" & strWhereCondition & "<-"
    DoCmd.OpenReport "rptEmpWork", acViewPreview, "qryEmpReport", strWhereCondition
    

    View the output from Debug.Print in the Immediate window; Ctrl+g will take you there.