gcarpenter
Registered User.
- Local time
- Today, 11:49
- Joined
- Oct 21, 2013
- Messages
- 68
I use the follwing code to filter a report based on the listbox selection on a form. Below is the code I use, the problem it will error if the results have an apostrophe in the string. Please help.
Private Sub FilterDesc_Click()
Dim strWhere As String
Dim ctl As Control
Dim varItem As Variant
'make sure a selection has been made
If Me.ListCarrier.ItemsSelected.Count = 0 Then
MsgBox "Must select at least 1 Carrier"
Exit Sub
End If
'add selected values to string
Set ctl = Me.ListCarrier
For Each varItem In ctl.ItemsSelected
' strWhere = strWhere & ctl.ItemData(varItem) & ","
'Use this line if your value is text
strWhere = strWhere & "'" & ctl.ItemData(varItem) & "',"
Next varItem
'trim trailing comma
strWhere = Left(strWhere, Len(strWhere) - 1)
'open the report, restricted to the selected items
DoCmd.OpenReport "rptPolyRezCost-Alt", acViewReport, , "Desc IN(" & strWhere & ")"
DoCmd.Close acForm, "PolyRezCostFilterEquip"
End Sub
Private Sub FilterDesc_Click()
Dim strWhere As String
Dim ctl As Control
Dim varItem As Variant
'make sure a selection has been made
If Me.ListCarrier.ItemsSelected.Count = 0 Then
MsgBox "Must select at least 1 Carrier"
Exit Sub
End If
'add selected values to string
Set ctl = Me.ListCarrier
For Each varItem In ctl.ItemsSelected
' strWhere = strWhere & ctl.ItemData(varItem) & ","
'Use this line if your value is text
strWhere = strWhere & "'" & ctl.ItemData(varItem) & "',"
Next varItem
'trim trailing comma
strWhere = Left(strWhere, Len(strWhere) - 1)
'open the report, restricted to the selected items
DoCmd.OpenReport "rptPolyRezCost-Alt", acViewReport, , "Desc IN(" & strWhere & ")"
DoCmd.Close acForm, "PolyRezCostFilterEquip"
End Sub