By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,475 Members | 1,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,475 IT Pros & Developers. It's quick & easy.

Search Criteria Error

P: 12
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 > "")

Share this Question
Share on Google+
5 Replies


P: 12
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

Expert 100+
P: 446
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
Expert Mod 100+
P: 2,321
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

Expert 100+
P: 446
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
Expert Mod 15k+
P: 31,186
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

Post your reply

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