G
george 16-17
Greetings,
I am very new to Access and VBA and in need of assistance. I have been
attempting to make a search form with two list boxes to filter my main data
table. The search form also has other text boxes to also filter.
I have been able to program one list box (cost center) that works fine, but
when I added a second list box (location) I receive a "runtime error '2448'"
and the debugger points to line
Me.Filter = strWhere.
The immediate window revealed:
[Cost Center] IN [Location] in ("FLPK") AND)
"FLPK" is one of the locations I tried to run. The string looks bad(?)...
Here is my code:
Private Sub cmdFilter_Click()
Dim strWhere As String
Dim lngLen As Long
Const conJetDate = "\#mm\/dd\/yyyy\#"
'List box code Location****************************************
Dim varItem As Variant 'Selected items
Dim strDescrip As String 'Description of WhereCondition
Dim strDelim As String 'Delimiter for this field type.
'Loop through the ItemsSelected in the list box.
strDelim = """" 'Delimiter appropriate to field type.
With Me.lstLocation
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 & ","
'Build up the description from the text in the visible
column. See note 2.
strDescrip = strDescrip & """" & .Column(1, varItem) & """, "
End If
Next
End With
'Remove trailing comma. Add field name, IN operator, and brackets.
lngLen = Len(strWhere) - 1
If lngLen > 0 Then
strWhere = "[Location] IN (" & Left$(strWhere, lngLen) & ") AND "
lngLen = Len(strDescrip) - 2
End If
'List box Cost Ctr******************************************
With Me.lstCostCtr
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 & ","
'Build up the description from the text in the visible
column.
strDescrip = strDescrip & """" & .Column(1, varItem) & """, "
End If
Next
End With
'Remove trailing comma. Add field name, IN operator, and brackets.
lngLen = Len(strWhere) - 1
If lngLen > 0 Then
strWhere = "[Cost Ctr] IN (" & Left$(strWhere, lngLen) & ") AND "
lngLen = Len(strDescrip) - 2
End If
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
MsgBox "No criteria", vbInformation, "Nothing to do."
Else
strWhere = Left$(strWhere, lngLen)
'For debugging, remove the leading quote on the next line. Prints to
Immediate Window (Ctrl+G).
'Debug.Print strWhere
'Finally, apply the string as the form's Filter.
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub
Thanks in advance and any assistance would be greatly appreciated,
george
I am very new to Access and VBA and in need of assistance. I have been
attempting to make a search form with two list boxes to filter my main data
table. The search form also has other text boxes to also filter.
I have been able to program one list box (cost center) that works fine, but
when I added a second list box (location) I receive a "runtime error '2448'"
and the debugger points to line
Me.Filter = strWhere.
The immediate window revealed:
[Cost Center] IN [Location] in ("FLPK") AND)
"FLPK" is one of the locations I tried to run. The string looks bad(?)...
Here is my code:
Private Sub cmdFilter_Click()
Dim strWhere As String
Dim lngLen As Long
Const conJetDate = "\#mm\/dd\/yyyy\#"
'List box code Location****************************************
Dim varItem As Variant 'Selected items
Dim strDescrip As String 'Description of WhereCondition
Dim strDelim As String 'Delimiter for this field type.
'Loop through the ItemsSelected in the list box.
strDelim = """" 'Delimiter appropriate to field type.
With Me.lstLocation
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 & ","
'Build up the description from the text in the visible
column. See note 2.
strDescrip = strDescrip & """" & .Column(1, varItem) & """, "
End If
Next
End With
'Remove trailing comma. Add field name, IN operator, and brackets.
lngLen = Len(strWhere) - 1
If lngLen > 0 Then
strWhere = "[Location] IN (" & Left$(strWhere, lngLen) & ") AND "
lngLen = Len(strDescrip) - 2
End If
'List box Cost Ctr******************************************
With Me.lstCostCtr
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 & ","
'Build up the description from the text in the visible
column.
strDescrip = strDescrip & """" & .Column(1, varItem) & """, "
End If
Next
End With
'Remove trailing comma. Add field name, IN operator, and brackets.
lngLen = Len(strWhere) - 1
If lngLen > 0 Then
strWhere = "[Cost Ctr] IN (" & Left$(strWhere, lngLen) & ") AND "
lngLen = Len(strDescrip) - 2
End If
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
MsgBox "No criteria", vbInformation, "Nothing to do."
Else
strWhere = Left$(strWhere, lngLen)
'For debugging, remove the leading quote on the next line. Prints to
Immediate Window (Ctrl+G).
'Debug.Print strWhere
'Finally, apply the string as the form's Filter.
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub
Thanks in advance and any assistance would be greatly appreciated,
george