473,407 Members | 2,314 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,407 software developers and data experts.

Search Criteria Error

I am using an example code to filter a form which lists records from a table.

The example database provided allows users to filter records with multiple criteria fields. However, since I have made the same code specific to the names of fields in my table, I receive an error "debug" message when I filter with more than one criteria.

Below is the "Filter" and "Filter Reset" code, with line 80 being highlighted as the problem.

Can anyone tell me what I might be doing wrong?

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdFilter_Click()
  2.     'Purpose:   Build up the criteria string from the non-blank search boxes, and apply to the form's Filter.
  3.     'Notes:     1. We tack " AND " on the end of each condition so you can easily add more search boxes; _
  4.                         we remove the trailing " AND " at the end.
  5.     '           2. The date range works like this: _
  6.                         Both dates      = only dates between (both inclusive. _
  7.                         Start date only = all dates from this one onwards; _
  8.                         End date only   = all dates up to (and including this one).
  9.     Dim strWhere As String                  'The criteria string.
  10.     Dim lngLen As Long                      'Length of the criteria string to append to.
  11.     Const conJetDate = "\#mm\/dd\/yyyy\#"   'The format expected for dates in a JET query string.
  12.  
  13.     '***********************************************************************
  14.     'Look at each search box, and build up the criteria string from the non-blank ones.
  15.     '***********************************************************************
  16.     'Text field example. Use quotes around the value in the string.
  17.  
  18.     'Customer Name
  19.     If Not IsNull(Me.txtFilterCustomerName) Then
  20.         strWhere = strWhere & "([Customer Name] = """ & Me.txtFilterCustomerName & """) AND "
  21.     End If
  22.  
  23.     'Hours
  24.     If Not IsNull(Me.txtFilterHours) Then
  25.         strWhere = strWhere & "([M1 Hours] = """ & Me.txtFilterHours & """) AND "
  26.     End If
  27.  
  28.     'Another text field example. Use Like to find anywhere in the field.
  29.  
  30.     'Serial No
  31.     If Not IsNull(Me.txtFilterSerial) Then
  32.         strWhere = strWhere & "([M1 Serial] Like ""*" & Me.txtFilterSerial & "*"") AND "
  33.     End If
  34.  
  35.     'Issues
  36.     If Not IsNull(Me.txtFilterIssues) Then
  37.         strWhere = strWhere & "([Issues] Like ""*" & Me.txtFilterIssues & "*"") AND "
  38.     End If
  39.  
  40.      'Actions
  41.     If Not IsNull(Me.txtFilterActions) Then
  42.         strWhere = strWhere & "([Details] Like ""*" & Me.txtFilterActions & "*"") AND "
  43.     End If
  44.  
  45.     'Number field example. Do not add the extra quotes.
  46.     'If Not IsNull(Me.cboFilterHours) Then
  47.         'strWhere = strWhere & "([M1 Hours] = " & Me.cboFilterHours & ") AND "
  48.     'End If
  49.  
  50.     'Yes/No field and combo example. If combo is blank or contains "ALL", we do nothing.
  51.     If Me.cboFilterFurtherAction = -1 Then
  52.         strWhere = strWhere & "([Further Action] = True) AND "
  53.     ElseIf Me.cboFilterFurtherAction = 0 Then
  54.         strWhere = strWhere & "([Further Action] = False) AND "
  55.     End If
  56.  
  57.     'Date field example. Use the format string to add the # delimiters and get the right international format.
  58.     If Not IsNull(Me.txtStartDate) Then
  59.         strWhere = strWhere & "([Visit Date] >= " & Format(Me.txtStartDate, conJetDate) & ") AND "
  60.     End If
  61.  
  62.     'Another date field example. Use "less than the next day" since this field has times as well as dates.
  63.     If Not IsNull(Me.txtEndDate) Then   'Less than the next day.
  64.         strWhere = strWhere & "([Visit Date] < " & Format(Me.txtEndDate + 1, conJetDate) & ") AND "
  65.     End If
  66.  
  67.     '***********************************************************************
  68.     'Chop off the trailing " AND ", and use the string as the form's Filter.
  69.     '***********************************************************************
  70.     'See if the string has more than 5 characters (a trailng " AND ") to remove.
  71.     lngLen = Len(strWhere) - 5
  72.     If lngLen <= 0 Then     'Nah: there was nothing in the string.
  73.         MsgBox "No criteria", vbInformation, "Nothing to do."
  74.     Else                    'Yep: there is something there, so remove the " AND " at the end.
  75.         strWhere = Left$(strWhere, lngLen)
  76.         'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
  77.         'Debug.Print strWhere
  78.  
  79.         'Finally, apply the string as the form's Filter.
  80.         Me.Filter = strWhere
  81.         Me.FilterOn = True
  82.     End If
  83. End Sub
  84.  
  85. Private Sub cmdReset_Click()
  86.     'Purpose:   Clear all the search boxes in the Form Header, and show all records again.
  87.     Dim ctl As Control
  88.  
  89.     'Clear all the controls in the Form Header section.
  90.     For Each ctl In Me.Section(acHeader).Controls
  91.         Select Case ctl.ControlType
  92.         Case acTextBox, acComboBox
  93.             ctl.value = Null
  94.         Case acCheckBox
  95.             ctl.value = False
  96.         End Select
  97.     Next
  98.  
  99.     'Remove the form's filter.
  100.     Me.FilterOn = False
  101. End Sub
Jan 9 '12 #1

✓ answered by NeoPa

Gwyn Jones:
This is purely a NUMBER field with a TEXT based criteria.
In that case I would say you should replace line #25 with :
Expand|Select|Wrap|Line Numbers
  1. strWhere = strWhere & "([M1 Hours] = " & Me.txtFilterHours & ") AND "
NB. Line #81 sets Me.FilterOn to True yet nowhere is it ever reset. I'd remove that line and add the following after the current line #82 :
Expand|Select|Wrap|Line Numbers
  1. Me.FilterOn = (strWhere > "")

5 1524
I think the problem may be with lines 23-26. This is purely a NUMBER field with a TEXT based criteria. It seems If I have take out these lines, the code works just fine.

Can anyone tell me how I should change this purely for a number field?

Many thanks.
Jan 9 '12 #2
sierra7
446 Expert 256MB
Hi
There is obviously something wrong with the syntax of the Filter String.

Line #77 will allow it to be printed in the debug window for viewing, however you may just write
Expand|Select|Wrap|Line Numbers
  1. MsgBox strWhere
to display in a messagebox.

To start with, check that string values have single quotes around them and dates have # around them, and that all fieldnames are spelt correctly.
S7
Jan 9 '12 #3
TheSmileyCoder
2,322 Expert Mod 2GB
If its a text based field match, you should use syntax like shown in lines 18-21.

If its a numeric match, you should use syntax like shown in lines 45-48.

When posting anything related to an error message, you should ALWAYS give the error number, as well as the error description.

If the above information does not help you, you will need to post the field types for all your fields.
Jan 9 '12 #4
sierra7
446 Expert 256MB
Hi Gwyn
It seems our messages crossed

If what is the content of Me.txtFilterHours? Whole hours, decimal hours or hours and minutes with a colon (:) separator?
S7
Jan 9 '12 #5
NeoPa
32,556 Expert Mod 16PB
Gwyn Jones:
This is purely a NUMBER field with a TEXT based criteria.
In that case I would say you should replace line #25 with :
Expand|Select|Wrap|Line Numbers
  1. strWhere = strWhere & "([M1 Hours] = " & Me.txtFilterHours & ") AND "
NB. Line #81 sets Me.FilterOn to True yet nowhere is it ever reset. I'd remove that line and add the following after the current line #82 :
Expand|Select|Wrap|Line Numbers
  1. Me.FilterOn = (strWhere > "")
Jan 9 '12 #6

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

Similar topics

1
by: Casey | last post by:
Hi, How would one allow for possible multiple search criteria using FREETEXTTABLE. For example, my table "listings" has a full-text search catalog, and I may want to: SELECT * FROM listings...
2
by: rinmanb70 | last post by:
I have a QBF form/query and a report from the QBF that shows the results of the QBF. I would like to show the criteria on the report that was used in the QBF to get the info on report. I can't...
0
by: venugopalsripada | last post by:
Hi, I use imaplib to download emails from my AOL accounts. I have 6 accounts. I am recieving ('NO', ) as a return value for search(None, 'ALL') for one of the accounts while all other acocunts are...
1
by: James890 | last post by:
I want to enable a user to enter a start and end date to define the period they want to search for records of members who joined on certain dates. Funny thing is...I've got it to work half of the...
3
by: AishaKhalfan | last post by:
Hi, :wave: I have many search criteria in my program, such as: search by name, search by age, search by gender, search by city the user can search by name only, or by name and age, or by...
0
by: Samita | last post by:
Hi , I am doing a search criteria for employee where i want to search employee by allotment year in a text box, that too by entering the last 2 digits of year in the text box. what is the logic...
1
by: Samita | last post by:
I want to do an employee search in an employeemaster page with email id as the search criteria in vb.net.How to achive that,any email validation is needed while searching?
5
by: LC2007 | last post by:
Hello, Can someone please help me on how to create Search Criteria form, i can't find exactly what i am looking for!!! i need a search button! but i can't find anything that can help me!
7
by: veliscorin | last post by:
hi experts, the topic is rather misleading but i couldn't find a better way to phrase it.. here's my scenario: Table A: Products Table B: Attributes (with a product_id to show which product it...
4
by: sanika1507 | last post by:
Can you help me out please ! This is a search Stored procedure that returns the list .of patients .i want to include the start and end date in the search criteria So you will need to accept the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.