S
sooz9
Hello. I followed Allen Browne 2006 example on filtering a form using
criteria and it works great with one problem.
For my critiera I have the user select from a drop down list (4 different
ones) with the exception of a date range. When I filter by any of my critera
using the drop down list the information is filtered correctly. However...
The problem is that the filtered infomation shows up as the correspoding
"autonumber" to the drop down word that was selected. For example, the Shift
selected is "Day", the filter shows "1" ....the autonumber for Day. If Night
is selected, it shows a "2"
I attached the code below:
Private Sub cmdFilter_Click()
'Purpose: Build up the criteria string form the non-blank search
boxes, and apply to the form's Filter.
'Notes: 1. We tack " AND " on the end of each condition so you can
easily add more search boxes; _
we remove the trailing " AND " at the end.
' 2. The date range works like this: _
Both dates = only dates between (both
inclusive. _
Start date only = all dates from this one onwards; _
End date only = all dates up to (and including
this one).
Dim strWhere As String 'The criteria string.
Dim lngLen As Long 'Length of the criteria string
to append to.
Const conJetDate = "\#mm\/dd\/yyyy\#" 'The format expected for dates
in a JET query string.
'***********************************************************************
'Look at each search box, and build up the criteria string from the
non-blank ones.
'***********************************************************************
'Number field example. Do not add the extra quotes.
If Not IsNull(Me.cboFilterRunType) Then
strWhere = strWhere & "([RunType] = " & Me.cboFilterRunType & ") AND "
End If
If Not IsNull(Me.cboFilterBLPrimaryReason) Then
strWhere = strWhere & "([BLPrimaryReason] = " &
Me.cboFilterBLPrimaryReason & ") AND "
End If
If Not IsNull(Me.cboFilterSecondaryReason) Then
strWhere = strWhere & "([SecondaryReason] = " &
Me.cboFilterSecondaryReason & ") AND "
End If
If Not IsNull(Me.cboFilterShift) Then
strWhere = strWhere & "([Shift] = " & Me.cboFilterShift & ") 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 & "([EnteredOn] >= " & 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 & "([EnteredOn] < " & Format(Me.txtEndDate + 1,
conJetDate) & ") AND "
End If
'***********************************************************************
'Chop off the trailing " AND ", and use the string as the form's Filter.
'***********************************************************************
'See if the string has more than 5 characters (a trailng " AND ") to
remove.
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then 'Nah: there was nothing in the string.
MsgBox "No criteria", vbInformation, "Nothing to do."
Else 'Yep: there is something there, so remove the "
AND " at the end.
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
criteria and it works great with one problem.
For my critiera I have the user select from a drop down list (4 different
ones) with the exception of a date range. When I filter by any of my critera
using the drop down list the information is filtered correctly. However...
The problem is that the filtered infomation shows up as the correspoding
"autonumber" to the drop down word that was selected. For example, the Shift
selected is "Day", the filter shows "1" ....the autonumber for Day. If Night
is selected, it shows a "2"
I attached the code below:
Private Sub cmdFilter_Click()
'Purpose: Build up the criteria string form the non-blank search
boxes, and apply to the form's Filter.
'Notes: 1. We tack " AND " on the end of each condition so you can
easily add more search boxes; _
we remove the trailing " AND " at the end.
' 2. The date range works like this: _
Both dates = only dates between (both
inclusive. _
Start date only = all dates from this one onwards; _
End date only = all dates up to (and including
this one).
Dim strWhere As String 'The criteria string.
Dim lngLen As Long 'Length of the criteria string
to append to.
Const conJetDate = "\#mm\/dd\/yyyy\#" 'The format expected for dates
in a JET query string.
'***********************************************************************
'Look at each search box, and build up the criteria string from the
non-blank ones.
'***********************************************************************
'Number field example. Do not add the extra quotes.
If Not IsNull(Me.cboFilterRunType) Then
strWhere = strWhere & "([RunType] = " & Me.cboFilterRunType & ") AND "
End If
If Not IsNull(Me.cboFilterBLPrimaryReason) Then
strWhere = strWhere & "([BLPrimaryReason] = " &
Me.cboFilterBLPrimaryReason & ") AND "
End If
If Not IsNull(Me.cboFilterSecondaryReason) Then
strWhere = strWhere & "([SecondaryReason] = " &
Me.cboFilterSecondaryReason & ") AND "
End If
If Not IsNull(Me.cboFilterShift) Then
strWhere = strWhere & "([Shift] = " & Me.cboFilterShift & ") 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 & "([EnteredOn] >= " & 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 & "([EnteredOn] < " & Format(Me.txtEndDate + 1,
conJetDate) & ") AND "
End If
'***********************************************************************
'Chop off the trailing " AND ", and use the string as the form's Filter.
'***********************************************************************
'See if the string has more than 5 characters (a trailng " AND ") to
remove.
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then 'Nah: there was nothing in the string.
MsgBox "No criteria", vbInformation, "Nothing to do."
Else 'Yep: there is something there, so remove the "
AND " at the end.
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