Kamis, 19 Mei 2011

Re: [MS_AccessPros] Filter not working correctly

 

Yup--sorry! Forgot to include it. Form_Load and btnSearch code below. Thanks! Connie

Private Sub Form_Load()

'Set default value of Beginning Date to 1 month before current date
tbBeginningDate.Value = DateAdd("d", -31, Date)

'Use code behind Search button to filter
btnSearch_Click
Proc_Exit:
Exit Sub ' or Exit Function
Proc_Err:
MsgBox Err.Description, , _
"ERROR " & Err.Number _
& " Form_Load"
Resume Proc_Exit
End Sub

Private Sub btnSearch_Click()
On Error GoTo Proc_Err
'Purpose: Build up the criteria string from 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.
Dim ctl As Control
Dim varItem As Variant
'***********************************************************************
'Look at each search box, and build up the criteria string from the non-blank ones.
'***********************************************************************
'Date field example. Use the format string to add the # delimiters and get the right international format.
If Not IsNull(Me.tbBeginningDate) Then
strWhere = strWhere & "([ActivityDate] >= " & Format(Me.tbBeginningDate, 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.tbEndDate) Then 'Less than the next day.
strWhere = strWhere & "([ActivityDate] < " & Format(Me.tbEndDate + 1, conJetDate) & ") AND "
End If

'Search TrackingActivity
If Not IsNull(Me.cboTrackingActID) Then
strWhere = strWhere & "([TrackingActID] = " & Me.cboTrackingActID & ") AND "
End If

'Search Property Name
If Not IsNull(Me.cboProperty) Then
strWhere = strWhere & "([ListID] = " & Me.cboProperty.Column(0) & ") AND "
End If

'Search Address
If Not IsNull(Me.cboAddress) Then
strWhere = strWhere & "([ListID] = " & Me.cboAddress.Column(0) & ") 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



Proc_Exit:
Exit Sub ' or Exit Function

Proc_Err:
MsgBox Err.Description, , _
"ERROR " & Err.Number _
& " btnSearch_Click"

Resume Proc_Exit

'if you want to single-step code to find error, CTRL-Break at MsgBox
'then set this to be the next statement by right-clicking on Resume Statement, then press F8 to execute one line at a time
Resume


End Sub

--- In MS_Access_Professionals@yahoogroups.com, Duane Hookom <duanehookom@...> wrote:
>
>
> Connie,
> Any chance you could share your code for " a filter is made in code "?
>
> Duane Hookom
> MS Access MVP
>
>
>
> From: no_reply@yahoogroups.com
>
>
>
>
>
> Hi all,
> Once again I'm using the header with unbound boxes to create a filter for the detail section of the form. Am encountering a problem:
>
> 1. Wrongs records returned if the Clear Filter button is not used before a new search. For example: The form loads and a filter is made in code to show records for the previous 31 days. That works. But when I fill in another cbobox to filter on TrackingActID, I get strange results. If the original 31 day filter had 9 records, when I add info to the unbound TrackingActID cbobox & press the Search button, the least current 5 records are returned irregardless of the trackingActID number. i.e. the original search results are returned minus the most current 4 records. If I click search again, it many return the same 5 records, but once I click several times 4 records are returned with the correct results. e
>
> I know I can add Me.FilterOn.Off at the beginning of the Search button's code, but I'd like to understand why. How long does strWhere retain information?
>
> thank you!
> Connie
>
>
>
>
> [Non-text portions of this message have been removed]
>

__._,_.___
Recent Activity:
.

__,_._,___

Tidak ada komentar:

Posting Komentar