If you have a Text field then it should be in this format:
You're using Not IsNull, but in the code I wrote it's Len(). Your code is fine if the Allow Zero Length property of the Text field is set to No.Code:strWhere = "[TheTextField] =[COLOR=red] '[/COLOR]" & Me.txtControlName & "[COLOR=red]'[/COLOR]"
I have extracted your code. However, the filter is not working. For example, I enter Start Cm Code: 246, End Cm Code: 246. It shows 246 and other Codes as well. I attach my entire codes below, can you have a look what is missing.
Code:
Private Sub cmdPreview_Click()
'On Error GoTo Err_Handler 'Remove the single quote from start of this line once you have it working.
'Purpose: Filter a report to a date range.
'Documentation: [URL]http://allenbrowne.com/casu-08.html[/URL]
'Note: Filter uses "less than the next day" in case the field has a time component.
Dim strReport As String
Dim strCmCodeField As String
Dim strWhere As String
Dim lngView As Long
'DO set the values in the next 3 lines.
strReport = "rptMainContractLabourCosting" 'Put your report name in these quotes.
strCmCodeField = "[CM Code]" 'Put your field name in the square brackets in these quotes.
lngView = acViewPreview 'Use acViewNormal to print instead of preview.
'Build the filter string.
If Len(Me.txtStartCmCode & vbNullString) = 0 Then
strWhere = "(" & strCmCodeField & " = ' " & (Me.txtStartCmCode & " ' ")
End If
If Len(Me.txtEndCmCode) Then
If strWhere <> vbNullString Then
strWhere = strWhere & " AND "
End If
If (Me.txtEndCmCode & vbNullString) = 0 Then
strWhere = "(" & strCmCodeField & " = ' " & (Me.txtEndCmCode & " ' ")
End If
'Close the report if already open: otherwise it won't filter properly.
If CurrentProject.AllReports(strReport).IsLoaded Then
DoCmd.Close acReport, strReport
End If
End If
'Open the report.
'Debug.Print strWhere 'Remove the single quote from the start of this line for debugging purposes.
DoCmd.OpenReport strReport, lngView, , strWhere
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "Cannot open report"
End If
Resume Exit_Handler
End Sub
Many thanks