Search code examples
ms-accessms-access-2007

Open Report based on text field


I have a form with a button and a text box as well as some other data. I need to open a report with calculated fields based on the value entered in the text box when the button is pressed. When a user enters a value into the text box it would open the report with only fields with that value.

Currently I am using

Private Sub Command11_Click()
DoCmd.OpenReport "Mileage", acViewPreview, , "Truck #" & Me.FrmTruck.Value & "'"
End Sub 

This would work if it did not try to find a text box on the opened report named Truck #(EnteredValue) I am sure I am missing something since I have done this before but I can't place on how to filter based on the text box.


Solution

  • Assuming the Record Source of that report includes a field named Truck #, enclose that name in square brackets when you create the WhereCondition option for OpenReport, ie [Truck #]

    Then, if the datatype of that field is numeric ...

    DoCmd.OpenReport "Mileage", acViewPreview, , "[Truck #] = " & Me.FrmTruck.Value
    

    Or if the datatype is text ...

    DoCmd.OpenReport "Mileage", acViewPreview, , "[Truck #] = '" & Me.FrmTruck.Value & "'"
    

    Note, if Me.FrmTruck is not the text box you mentioned where the user enters their search value, replace it with the correct text box name.