Skip to content

Filtering a Report by Order Number

A reader wants to know how to get a report to display just the items related to a specific order.

Q:  George Adams is having a problem with getting a report to display just the items related to a specific order.  How can this be done?

 

A:  You need to filter the report for the matching items, using an OrderID or other key field.  One way is to create a version of the report with a criterion on the key field that matches the key field on the form; another is to create a filter string in code and apply it to the report.  Here is a procedure that uses the filter string method:

Private Sub cmdPrintOrder_Click()

 

On Error GoTo ErrorHandler

  

   Dim lngOrderID As Long

   Dim strCaption As String

   Dim strReport As String

   Dim rpt As Access.Report

  

   lngOrderID = Nz(Me![OrderID])

   If lngOrderID = 0 Then

      GoTo ErrorHandlerExit

   Else

      strReport = “rptSingleOrder”

      strCaption = “Order No. ” & lngOrderID

   End If

  

   DoCmd.OpenReport reportname:=strReport, _

      View:=acViewPreview, _

      windowmode:=acHidden

   Set rpt = Reports(strReport)

   rpt.Caption = strCaption

   DoCmd.OpenReport reportname:=strReport, _

      View:=acNormal

  

ErrorHandlerExit:

   Exit Sub

 

ErrorHandler:

   MsgBox “Error No: ” & Err.Number & “; Description: ” & _

      Err.Description

   Resume ErrorHandlerExit

 

End Sub

About this author