473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Multiselect Form Filter Syntax Error

4
I'm having difficulty filtering a form using a multiselect list box when using some Allen Browne code. I get the error "Syntax Error (missing operator)in query expression '([City] IN (""Sydney"London)'. - when I filter the cities Sydney and London from the multiselect list box in the form. The list box multiselect is set to Simple. The code breaks at Me.Filter = strWhere four lines from the end. Thanks for looking!

__________________________________________________ _______
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
Dim strCity As String
Dim varSelected As Variant
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.
'************************************************* **********************
'Text field example. Use quotes around the value in the string.
If Me.txtFilterCity.ItemsSelected.Count > 0 Then
For Each varSelected In Me.txtFilterCity.ItemsSelected
strCity = strCity & """" & Me.txtFilterCity.ItemData(varSelected)
Next varSelected
strWhere = strWhere & "([City] IN (" & strCity & ") AND "
End If

If Not IsNull(Me.txtFilterCity) Then
strWhere = strWhere & "([City] = """ & Me.txtFilterCity & """) AND "
End If

'Another text field example. Use Like to find anywhere in the field.
If Not IsNull(Me.txtFilterMainName) Then
strWhere = strWhere & "([MainName] Like ""*" & Me.txtFilterMainName & "*"") AND "
End If

'Number field example. Do not add the extra quotes.
If Not IsNull(Me.cboFilterLevel) Then
strWhere = strWhere & "([LevelID] = " & Me.cboFilterLevel & ") AND "
End If

'Yes/No field and combo example. If combo is blank or contains "ALL", we do nothing.
If Me.cboFilterIsCorporate = -1 Then
strWhere = strWhere & "([IsCorporate] = True) AND "
ElseIf Me.cboFilterIsCorporate = 0 Then
strWhere = strWhere & "([IsCorporate] = False) 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
Aug 17 '10 #1
1 1871
MikeTheBike
639 Expert 512MB
Hi

Perhaps you could modify you loop like this
Expand|Select|Wrap|Line Numbers
  1.     For Each varSelected In Me.txtFilterCity.ItemsSelected
  2.         strCity = strCity & ",""" & Me.txtFilterCity.ItemData(varSelected) & i & """"
  3.     Next varSelected
  4.  
  5.     strCity = Mid(strCity, 2)  'REMOVES LEADING COMMA
  6.  
  7.     strWhere = strWhere & "([City] IN (" & strCity & ") AND "
I think that may get rid of the that particulare error (but there may be more!).


MTB
Aug 18 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Simon | last post by:
Dear reader, Under Tools>>Options>>Tables/Queries there is a setting possible of "SQL Server Compatible Syntax (ANSI 92)". The two tick boxes here are · "This database" · ...
7
by: jodyblau | last post by:
I'm new at this stuff, so bare with me. I have a main form which displays a client record. The main form has 2 subforms. The first subform has a list of Cases. The Second subform is a list of...
6
by: Dave Richardson | last post by:
Hi to everyone, I have the following problem which is producing the following error: Syntax error (missing operator) in query expression '(CourseNo = CN1863) This caused from the follOwing...
17
by: trose178 | last post by:
Good day all, I am working on a multi-select list box for a standard question checklist database and I am running into a syntax error in the code that I cannot seem to correct. I will also note...
5
by: OzNet | last post by:
Can someone tell me what I am doing wrong here please? I have a form with two unbound text boxes formatted to short date and an OK button. My button code is as follows: Private Sub...
5
by: phill86 | last post by:
Hi, I have a form that I have applied a filter to by using combo boxes which works fine. Is there a way to apply that filter to the forms underlying query Here is the code that I use to...
7
by: HSXWillH | last post by:
I have a field in a database that contains last names. In some of those names, like O'Brien and O'Connor, there is a ' symbol. I am using combo boxes on a form to build a form filter and in...
5
by: gershwyn | last post by:
I'm trying to make a very simple order form. I have two tables - OrderHeader has information specific to the order itself, such as company name and order date. OrderDetail contains details such as...
2
by: Lieven | last post by:
The problem is fairly complex. I have a table where I can add records. These records have several parts. Each record consists of ID, Date, Train Number and many more. I have a form where I can...
28
by: matt753 | last post by:
I want to impliment a button that filters and shows only records where the field "PerformanceID" has nothing in it. Currently have the following for filtering by date, I know the syntax is similar...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.