473,499 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search Criteria Error

12 New Member
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
5 1525
Gwyn Jones
12 New Member
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 Recognized Expert Contributor
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 Recognized Expert Moderator Top Contributor
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 Recognized Expert Contributor
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,557 Recognized Expert Moderator MVP
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
8137
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
4207
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
1633
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
3936
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
2428
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
1053
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
1109
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
2196
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
1995
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
1778
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
7009
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
7223
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...
1
6899
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
5475
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,...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.