I'm trying to get information from a list box to run a report to select criteria. But I can't get the SQL statement right. I copied this code off the internet and I'm trying to configure it to work for my report.
I'm getting a "Error 3705 =- Syntax Error Missing operator in query expression
http://allenbrowne.com/ser-50.html
Private Sub cmdQuery_Click()
On Error GoTo Err_Handler
'Purpose: Open the report filtered to the items selected in the list box.
Dim varItem As Variant 'Selected items
Dim strWhere As String 'String to use as WhereCondition
Dim lngLen As Long 'Length of string
Dim strDelim As String 'Delimiter for this field type.
Dim strDoc As String 'Name of report to open.
strDelim = """" 'Delimiter appropriate to field type. See note 1.
strDoc = "Comprehensive Regional Results Events"
'Loop through the ItemsSelected in the list box.
With Me.lstRegion
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column (hidden).
strWhere = strWhere & strDelim & .ItemData(varItem) & strDelim & ","
End If
Next
End With
'Remove trailing comma. Add field name, IN operator, and brackets.
'lngLen = Len(strWhere) - 1
If lngLen > 0 Then
'This is the problem line
strWhere = "[Region] IN (" & Left$(strWhere, lngLen) & ")"
lngLen = Len(strDescrip) - 2
End If
'Report will not filter if open, so close it.
If CurrentProject.AllReports(strDoc).IsLoaded Then
DoCmd.Close acReport, strDoc
End If
'Omit the last argument for Access 2000 and earlier. See note 4.
DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere, OpenArgs:=strDescrip
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then 'Ignore "Report cancelled" error.
MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdPreview_Click"
End If
Resume Exit_Handler
End Sub
I'm getting a "Error 3705 =- Syntax Error Missing operator in query expression
http://allenbrowne.com/ser-50.html
Private Sub cmdQuery_Click()
On Error GoTo Err_Handler
'Purpose: Open the report filtered to the items selected in the list box.
Dim varItem As Variant 'Selected items
Dim strWhere As String 'String to use as WhereCondition
Dim lngLen As Long 'Length of string
Dim strDelim As String 'Delimiter for this field type.
Dim strDoc As String 'Name of report to open.
strDelim = """" 'Delimiter appropriate to field type. See note 1.
strDoc = "Comprehensive Regional Results Events"
'Loop through the ItemsSelected in the list box.
With Me.lstRegion
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column (hidden).
strWhere = strWhere & strDelim & .ItemData(varItem) & strDelim & ","
End If
Next
End With
'Remove trailing comma. Add field name, IN operator, and brackets.
'lngLen = Len(strWhere) - 1
If lngLen > 0 Then
'This is the problem line
strWhere = "[Region] IN (" & Left$(strWhere, lngLen) & ")"
lngLen = Len(strDescrip) - 2
End If
'Report will not filter if open, so close it.
If CurrentProject.AllReports(strDoc).IsLoaded Then
DoCmd.Close acReport, strDoc
End If
'Omit the last argument for Access 2000 and earlier. See note 4.
DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere, OpenArgs:=strDescrip
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then 'Ignore "Report cancelled" error.
MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdPreview_Click"
End If
Resume Exit_Handler
End Sub