Lavenderchan
Technical User
Hello I am getting a syntax error in my code while tying to filer with a list box.
Here is my code:
Sub Search2()
Dim strSearch, strCriteria, strBuilding_Type, strCounty As String
Dim Task As String
Dim varItem As Variant
'''=========== code for Building Type List box
For Each varItem In Me!ListBuildingType.ItemsSelected
''' use ASCII Chr(34) to replace Double Quotes in VB
'''Chr(34) = "
''' Use "Or" to get result from all states
strBuilding_Type = strBuilding_Type & "[Building_Type] = " & Chr(34) & Me!ListBuildingType.ItemData(varItem) & Chr(34) & "Or"
Next varItem
MsgBox (strBuilding_Type)
If Len(strBuilding_Type) > 0 Then
strBuilding_Type = Left(strBuilding_Type, Len(strBuilding_Type) - 2)
strCriteria = strCriteria & " And (" & strBuilding_Type & ")"
End If
MsgBox (strCriteria)
'''=========== code for Customer type combined from list box and combo box
If IsNull(Me.cboCounty) Or Me.cboCounty = "" Then
' do nothing
ElseIf Me.cboCounty = 390 Then ' for Select Multiple
strSearch = "[CountyID] in (" & TempVars!tempcounty & ")"
strCriteria = strCriteria & " And (" & strSearch & ")"
Else
strSearch = "([CountyID] = " & Me.cboCounty & ")" ''' for item selected from combo box
strCriteria = strCriteria & " And (" & strSearch & ")"
End If
Task = "select * from [tblComparison Data] where (" & strCriteria & ")"
MsgBox Task
Me.FilterOn = True
[highlight #FCE94F]DoCmd.ApplyFilter Task[/highlight]
End Sub
There error is happening at the apply filter task
For each of the msgbox boxes I have added to figure out where the error it self is coming from I am not sure what is causing it. the error states Syntax Error (Missing operator) in Query Expression '(And([Building_Type]="3"Or[Building_Type]="8"))'.
Any information to help fix this is greatly appreciated.
Here is my code:
Sub Search2()
Dim strSearch, strCriteria, strBuilding_Type, strCounty As String
Dim Task As String
Dim varItem As Variant
'''=========== code for Building Type List box
For Each varItem In Me!ListBuildingType.ItemsSelected
''' use ASCII Chr(34) to replace Double Quotes in VB
'''Chr(34) = "
''' Use "Or" to get result from all states
strBuilding_Type = strBuilding_Type & "[Building_Type] = " & Chr(34) & Me!ListBuildingType.ItemData(varItem) & Chr(34) & "Or"
Next varItem
MsgBox (strBuilding_Type)
If Len(strBuilding_Type) > 0 Then
strBuilding_Type = Left(strBuilding_Type, Len(strBuilding_Type) - 2)
strCriteria = strCriteria & " And (" & strBuilding_Type & ")"
End If
MsgBox (strCriteria)
'''=========== code for Customer type combined from list box and combo box
If IsNull(Me.cboCounty) Or Me.cboCounty = "" Then
' do nothing
ElseIf Me.cboCounty = 390 Then ' for Select Multiple
strSearch = "[CountyID] in (" & TempVars!tempcounty & ")"
strCriteria = strCriteria & " And (" & strSearch & ")"
Else
strSearch = "([CountyID] = " & Me.cboCounty & ")" ''' for item selected from combo box
strCriteria = strCriteria & " And (" & strSearch & ")"
End If
Task = "select * from [tblComparison Data] where (" & strCriteria & ")"
MsgBox Task
Me.FilterOn = True
[highlight #FCE94F]DoCmd.ApplyFilter Task[/highlight]
End Sub
There error is happening at the apply filter task
For each of the msgbox boxes I have added to figure out where the error it self is coming from I am not sure what is causing it. the error states Syntax Error (Missing operator) in Query Expression '(And([Building_Type]="3"Or[Building_Type]="8"))'.
Any information to help fix this is greatly appreciated.