Search code examples
ms-accessparametersvbareportms-access-2010

doCmd.OpenReport where-condition syntax


I have a very small Access database where I'd like to have a button, which opens up a report ("Mitarbeiterhonorare") filtered by a chosen value in the combination field "Kf-Referentenname". Unfortunately, my where-condition just doesn't seem to work (there has to be some formatting mistake, since it just opens up an empty report). Can anybody help me with the syntax there?

Thanks a lot!

Private Sub cmdOpenReport_Click()
    If IsNull(Me.Kf_Referentenname) Then
        Me.Kf_Referentenname.SetFocus
        MsgBox "Bitte einen Mitarbeiter auswählen!", vbExclamation
    Else
        DoCmd.OpenReport ReportName:="Mitarbeiterhonorare",      View:=acViewPreview, _
            WhereCondition:=Referent_Name = "& Me.Kf_Referentenname"
    End If
End Sub

The query the report is based on Looks like this:

SELECT Sum(Honorare.Betrag) AS Gesamtbetrag_Honorare, Mitarbeiter.Referent_Name FROM Mitarbeiter INNER JOIN Honorare ON Mitarbeiter.Mitarbeiter_ID = Honorare.Mitarbeiter_ID GROUP BY Honorare.Mitarbeiter_ID, Mitarbeiter.Referent_Name;

Solution

  • This shouldn't even compile *, the double quotes are wrong.

    WhereCondition must be a string:

       DoCmd.OpenReport ReportName:="Mitarbeiterhonorare", View:=acViewPreview, _
            WhereCondition:="Referent_Name = " & Me.Kf_Referentenname
    

    and then Referent_Name is probably a text field, so you need additional quotes around the parameter:

        DoCmd.OpenReport ReportName:="Mitarbeiterhonorare", View:=acViewPreview, _
            WhereCondition:="Referent_Name = '" & Me.Kf_Referentenname & "'"
    

    (*) Do you have Option Explicit at the top of your module?

    It enforces variable declaration and reports undeclared or misspelled variables/constants at compile time. To have this automatically in new modules, set the Require Variable Declaration option in the VBA Editor.
    This is really a must have for VBA development.