Kamis, 21 Juli 2011

[MS_AccessPros] Trouble with quotes etc. in creating filter in code

 

I'm using Allene Browne's Filter Form. Had it working. Now I'm trying to add a criteria and can't get it right. So frustrating--it's simpler than the rest and I know I should be able to create it but ...

The query that the form is based has a column "CurrentListing" which has a Y or N. (Not -1 or 0). Whenever the form is loaded I want it to be automatically filtered for only the record where CurrentListing = Y. The Load event calls btnSearch_Click. That is working. In btnSearch_Click I am trying to add CurrentListing = Y to the filter all the time.

Here's what I have now. I have tried many other things and to no avail. The line re CurrentListing filters out everything. If I remove it, the filter works correctly.

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.
'***********************************************************************
'Only use Current Listings
strWhere = strWhere & "([CurrentListing] = Y) AND "

'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

' If a filter currently applied,
If Me.FilterOn = True Then
' Remove the old filter
Me.Filter = ""
Me.FilterOn = False
End If

'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

__._,_.___
Recent Activity:
.

__,_._,___

Tidak ada komentar:

Posting Komentar