G
george 16-17
Greetings,
I am very new to Access and VBA. I am trying to add a multiselect list box
(http://allenbrowne.com/ser-50.html) and code to a search form
(http://allenbrowne.com/ser-62.html) that I modified based on Allen Browne's
examples, but I can not seen to get the code right, as I keep on getting a
runtime error. The list box (lstLocation) filters the “Location†field in the
report section.
Any help would be appreciated.
Here is my code:
Private Sub cmdFilter_Click()
Dim strWhere As String
Dim lngLen As Long
Const conJetDate = "\#mm\/dd\/yyyy\#"
'List box code**********************************************
Dim varItem As Variant
Dim strDescrip As String
Dim strDelim As String
strDelim = """"
'Loop through the ItemsSelected in the list box.
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) & ")"
lngLen = Len(strDescrip) - 2
End If
If Not IsNull(Me.txtCostCenter) Then
strWhere = strWhere & "([Cost Ctr] = """ & Me.txtCostCenter & """)
AND "
End If
If Not IsNull(Me.txtDeptName) Then
strWhere = strWhere & "([Dept Name] Like ""*" & Me.txtDeptName &
"*"") AND "
End If
'Another text field example. Use Like to find anywhere in the field.
If Not IsNull(Me.txtEmpName) Then
strWhere = strWhere & "([Name] Like ""*" & Me.txtEmpName & "*"") AND "
End If
If Not IsNull(Me.txtInjuryCode) Then
strWhere = strWhere & "([OMS Inj Code] Like """ & Me.txtInjuryCode &
"*"") AND "
End If
'Date field example. Use the format string to add the # delimiters and
get the right international format.
If Not IsNull(Me.txtStartDate) Then
strWhere = strWhere & "([Date of Inj] >= " & Format(Me.txtStartDate,
conJetDate) & ") AND "
End If
'Another date field example. Use "less than the next day" since this
field has times as well as dates.
If Not IsNull(Me.txtEndDate) Then 'Less than the next day.
strWhere = strWhere & "([Date of Inj] < " & Format(Me.txtEndDate +
1, conJetDate) & ") AND "
End If
'See if the string has more than 5 characters (a trailng " AND ") to
remove.
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
MsgBox "No criteria", vbInformation, "Nothing to do."
Else .
strWhere = Left$(strWhere, lngLen)
'Debug.Print strWhere
'Finally, apply the string as the form's Filter.
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub
Thanks
I am very new to Access and VBA. I am trying to add a multiselect list box
(http://allenbrowne.com/ser-50.html) and code to a search form
(http://allenbrowne.com/ser-62.html) that I modified based on Allen Browne's
examples, but I can not seen to get the code right, as I keep on getting a
runtime error. The list box (lstLocation) filters the “Location†field in the
report section.
Any help would be appreciated.
Here is my code:
Private Sub cmdFilter_Click()
Dim strWhere As String
Dim lngLen As Long
Const conJetDate = "\#mm\/dd\/yyyy\#"
'List box code**********************************************
Dim varItem As Variant
Dim strDescrip As String
Dim strDelim As String
strDelim = """"
'Loop through the ItemsSelected in the list box.
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) & ")"
lngLen = Len(strDescrip) - 2
End If
If Not IsNull(Me.txtCostCenter) Then
strWhere = strWhere & "([Cost Ctr] = """ & Me.txtCostCenter & """)
AND "
End If
If Not IsNull(Me.txtDeptName) Then
strWhere = strWhere & "([Dept Name] Like ""*" & Me.txtDeptName &
"*"") AND "
End If
'Another text field example. Use Like to find anywhere in the field.
If Not IsNull(Me.txtEmpName) Then
strWhere = strWhere & "([Name] Like ""*" & Me.txtEmpName & "*"") AND "
End If
If Not IsNull(Me.txtInjuryCode) Then
strWhere = strWhere & "([OMS Inj Code] Like """ & Me.txtInjuryCode &
"*"") AND "
End If
'Date field example. Use the format string to add the # delimiters and
get the right international format.
If Not IsNull(Me.txtStartDate) Then
strWhere = strWhere & "([Date of Inj] >= " & Format(Me.txtStartDate,
conJetDate) & ") AND "
End If
'Another date field example. Use "less than the next day" since this
field has times as well as dates.
If Not IsNull(Me.txtEndDate) Then 'Less than the next day.
strWhere = strWhere & "([Date of Inj] < " & Format(Me.txtEndDate +
1, conJetDate) & ") AND "
End If
'See if the string has more than 5 characters (a trailng " AND ") to
remove.
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
MsgBox "No criteria", vbInformation, "Nothing to do."
Else .
strWhere = Left$(strWhere, lngLen)
'Debug.Print strWhere
'Finally, apply the string as the form's Filter.
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub
Thanks