Search code examples
vbams-accessreport

Data type mismatch in criteria expression error while trying to filter report


I just started with Access not too long ago and I'm having some trouble.

I am trying to filter a report that displays tracking numbers and associated data scanned in from barcodes on boxes. The tracking number is the primary key and it is stored as Short Text in the table.

I have a command button set up in a form that launches an input box to type or scan in the tracking number.

Whenever try to run this I get a

Run-time error '3464: Data type mismatch in the criteria expression.

My code:

Private Sub Command28_Click()
  Dim ReportName As String
  ReportName = "bucketContents"
  Dim trackingNum As String
  trackingNum = InputBox("Enter the Tracking number:", "Tracking Number Input")

  'if a value was entered, open and filter report
  If Len(Trim(trackingNum)) > 0 Then
    DoCmd.OpenReport ReportName, acViewPreview, , "[sampleID] = " & trackingNum
  End If
End Sub

Solution

  • Text values must be wrapped in quotes:

    DoCmd.OpenReport ReportName, acViewPreview, , "[sampleID] = '" & trackingNum & "'"