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

Help with this filter code please...

P: 6
After days of searching I finally an example that would work with my application, the only problem is after entering all of the code it is not working. Would someone be kind enough to take a look at this...

Exmaple web site: http://allenbrowne.com/ser-62.html

Expand|Select|Wrap|Line Numbers
  1. My Code: 
  2. 'Purpose:   This module illustrates how to create a search form, _
  3.             where the user can enter as many or few criteria as they wish, _
  4.             and results are shown one per line.
  5. 'Note:      Only records matching ALL of the criteria are returned.
  6. 'Author:    Allen Browne (allen@allenbrowne.com), June 2006.
  7. Option Compare Database
  8. Option Explicit
  9. Private Sub cmdbutReset_Click()
  10.     'Purpose: Clear all the search boxes in the Form Header, and show all records again.
  11.     Dim ctl As Control
  12.  
  13.     'Clear all the controls in the Form Header section.
  14.     For Each ctl In Me.Section(acHeader).Controls
  15.         Select Case ctl.ControlType
  16.         Case acTextBox
  17.             ctl.Value = Null
  18.         Case acCheckBox
  19.             ctl.Value = False
  20.         End Select
  21.     Next
  22.  
  23.     'Remove the form's filter.
  24.     Me.FilterOn = False
  25. End Sub
  26.  
  27. Private Sub Form_BeforeInsert(Cancel As Integer)
  28.     'To avoid problems if the filter returns no records, we did not set its AllowAdditions to No.
  29.     'We prevent new records by cancelling the form's BeforeInsert event instead.
  30.     'The problems are explained at http://allenbrowne.com/bug-06.html
  31.     Cancel = True
  32.     MsgBox "Record Not Found.", vbInformation, "Permission denied."
  33. End Sub
  34. Private Sub Form_Open(Cancel As Integer)
  35.     'Remove the single quote from these lines if you want to initially show no records.
  36.     'Me.Filter = "(False)"
  37.     'Me.FilterOn = True
  38. End Sub
  39. Private Sub cmdSearch_Click()
  40.     'Purpose:   Build up the criteria string form the non-blank search boxes, and apply to the form's Filter.
  41.     'Notes:     1. We tack " AND " on the end of each condition so you can easily add more search boxes; _
  42.                     we remove the trailing " AND " at the end.
  43.     '           2. The date range works like this: _
  44.                         Both dates      = only dates between (both inclusive. _
  45.                         Start date only = all dates from this one onwards; _
  46.                         End date only   = all dates up to (and including this one).
  47.     Dim strWhere As String                  'The criteria string.
  48.     Dim lngLen As Long                      'Length of the criteria string to append to.
  49.     Const conJetDate = "\#mm\/dd\/yyyy\#"   'The format expected for dates in a JET query string.
  50.  
  51.     '***********************************************************************
  52.     'Look at each search box, and build up the criteria string from the non-blank ones.
  53.     '***********************************************************************
  54.     'Date field example. Use the format string to add the # delimiters and get the right international format.
  55.     If Not IsNull(Me.tbxDate) Then
  56.         strWhere = strWhere & "([tbxCaseSearchDate] >= " & Format(Me.tbxDate, conJetDate) & ") AND "
  57.     End If
  58.  
  59.     'Text field example. Use quotes around the value in the string.
  60.     If Not IsNull(Me.tbxCase_) Then
  61.         strWhere = strWhere & "([tbxCaseSearchCase#] = ""*" & Me.tbxCase_ & "*"") AND "
  62.     End If
  63.  
  64.     'Text field example. Use quotes around the value in the string.
  65.     If Not IsNull(Me.tbxOfficer) Then
  66.         strWhere = strWhere & "([tbxCaseSearchOfficer] = ""*" & Me.tbxOfficer & "*"") AND "
  67.     End If
  68.  
  69.     'Text field example. Use quotes around the value in the string.
  70.     If Not IsNull(Me.tbxOfficer_) Then
  71.         strWhere = strWhere & "([tbxCaseSearchOfficer_] = ""*" & Me.tbxOfficer_ & "*"") AND "
  72.     End If
  73.  
  74.     'Another text field example. Use Like to find anywhere in the field.
  75.     If Not IsNull(Me.tbxIncidentType) Then
  76.         strWhere = strWhere & "([tbxCaseSearchIncidentType] Like ""*" & Me.tbxIncidentType & "*"") AND "
  77.     End If
  78.  
  79.      'Another text field example. Use Like to find anywhere in the field.
  80.     If Not IsNull(Me.tbxDescription) Then
  81.         strWhere = strWhere & "([tbxCaseSearchDescription] Like ""*" & Me.tbxDescription & "*"") AND "
  82.     End If
  83.  
  84.     'Number field example. Do not add the extra quotes.
  85.     'If Not IsNull(Me.tbxOfficer_) Then
  86.         'strWhere = strWhere & "([tbxCaseSearchOfficer_] = " & Me.tbxOfficer_ & ") AND "
  87.     'End If
  88.  
  89.     '***********************************************************************
  90.     'Chop off the trailing " AND ", and use the string as the form's Filter.
  91.     '***********************************************************************
  92.     'See if the string has more than 5 characters (a trailng " AND ") to remove.
  93.     lngLen = Len(strWhere) - 5
  94.     If lngLen <= 0 Then     'Nah: there was nothing in the string.
  95.         MsgBox "No criteria", vbInformation, "Nothing to do."
  96.     Else                    'Yep: there is something there, so remove the " AND " at the end.
  97.         strWhere = Left$(strWhere, lngLen)
  98.         'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
  99.         'Debug.Print strWhere
  100.  
  101.         'Finally, apply the string as the form's Filter.
  102.         Me.Filter = strWhere
  103.         Me.FilterOn = True
  104.     End If
  105. End Sub
Dec 25 '06 #1
Share this Question
Share on Google+
5 Replies


PEB
Expert 100+
P: 1,418
PEB
Do u have the respective form?

Here there is also a form that should be created! If u don't create it can't work!
Dec 26 '06 #2

PEB
Expert 100+
P: 1,418
PEB
In fact this code is for special application!

If u see carefully u will see that there is fields like:

tbxDate
tbxCaseSearchDate

tbxCase
tbxCaseSearchCase

tbxOfficer
tbxCaseSearchOfficer

That certenly u don't need them and u should change them with yours respective fields

There is also a Form named Form

and 2 boutons:

cmdbutReset
cmdSearch

So this code is customized for specified form! If u need to use it u can create your own form and catch the idea from this code...

1. How to create a search string
2. How to check the different kind of data typoe fields
3.How to set the filter to the form

Get the needed commands and create your own form using them!

Marry Cristmas!

After days of searching I finally an example that would work with my application, the only problem is after entering all of the code it is not working. Would someone be kind enough to take a look at this...

Exmaple web site: http://allenbrowne.com/ser-62.html

Expand|Select|Wrap|Line Numbers
  1. My Code: 
  2. 'Purpose:   This module illustrates how to create a search form, _
  3.             where the user can enter as many or few criteria as they wish, _
  4.             and results are shown one per line.
  5. 'Note:      Only records matching ALL of the criteria are returned.
  6. 'Author:    Allen Browne (allen@allenbrowne.com), June 2006.
  7. Option Compare Database
  8. Option Explicit
  9. Private Sub cmdbutReset_Click()
  10.     'Purpose: Clear all the search boxes in the Form Header, and show all records again.
  11.     Dim ctl As Control
  12.  
  13.     'Clear all the controls in the Form Header section.
  14.     For Each ctl In Me.Section(acHeader).Controls
  15.         Select Case ctl.ControlType
  16.         Case acTextBox
  17.             ctl.Value = Null
  18.         Case acCheckBox
  19.             ctl.Value = False
  20.         End Select
  21.     Next
  22.  
  23.     'Remove the form's filter.
  24.     Me.FilterOn = False
  25. End Sub
  26.  
  27. Private Sub Form_BeforeInsert(Cancel As Integer)
  28.     'To avoid problems if the filter returns no records, we did not set its AllowAdditions to No.
  29.     'We prevent new records by cancelling the form's BeforeInsert event instead.
  30.     'The problems are explained at http://allenbrowne.com/bug-06.html
  31.     Cancel = True
  32.     MsgBox "Record Not Found.", vbInformation, "Permission denied."
  33. End Sub
  34. Private Sub Form_Open(Cancel As Integer)
  35.     'Remove the single quote from these lines if you want to initially show no records.
  36.     'Me.Filter = "(False)"
  37.     'Me.FilterOn = True
  38. End Sub
  39. Private Sub cmdSearch_Click()
  40.     'Purpose:   Build up the criteria string form the non-blank search boxes, and apply to the form's Filter.
  41.     'Notes:     1. We tack " AND " on the end of each condition so you can easily add more search boxes; _
  42.                     we remove the trailing " AND " at the end.
  43.     '           2. The date range works like this: _
  44.                         Both dates      = only dates between (both inclusive. _
  45.                         Start date only = all dates from this one onwards; _
  46.                         End date only   = all dates up to (and including this one).
  47.     Dim strWhere As String                  'The criteria string.
  48.     Dim lngLen As Long                      'Length of the criteria string to append to.
  49.     Const conJetDate = "\#mm\/dd\/yyyy\#"   'The format expected for dates in a JET query string.
  50.  
  51.     '***********************************************************************
  52.     'Look at each search box, and build up the criteria string from the non-blank ones.
  53.     '***********************************************************************
  54.     'Date field example. Use the format string to add the # delimiters and get the right international format.
  55.     If Not IsNull(Me.tbxDate) Then
  56.         strWhere = strWhere & "([tbxCaseSearchDate] >= " & Format(Me.tbxDate, conJetDate) & ") AND "
  57.     End If
  58.  
  59.     'Text field example. Use quotes around the value in the string.
  60.     If Not IsNull(Me.tbxCase_) Then
  61.         strWhere = strWhere & "([tbxCaseSearchCase#] = ""*" & Me.tbxCase_ & "*"") AND "
  62.     End If
  63.  
  64.     'Text field example. Use quotes around the value in the string.
  65.     If Not IsNull(Me.tbxOfficer) Then
  66.         strWhere = strWhere & "([tbxCaseSearchOfficer] = ""*" & Me.tbxOfficer & "*"") AND "
  67.     End If
  68.  
  69.     'Text field example. Use quotes around the value in the string.
  70.     If Not IsNull(Me.tbxOfficer_) Then
  71.         strWhere = strWhere & "([tbxCaseSearchOfficer_] = ""*" & Me.tbxOfficer_ & "*"") AND "
  72.     End If
  73.  
  74.     'Another text field example. Use Like to find anywhere in the field.
  75.     If Not IsNull(Me.tbxIncidentType) Then
  76.         strWhere = strWhere & "([tbxCaseSearchIncidentType] Like ""*" & Me.tbxIncidentType & "*"") AND "
  77.     End If
  78.  
  79.      'Another text field example. Use Like to find anywhere in the field.
  80.     If Not IsNull(Me.tbxDescription) Then
  81.         strWhere = strWhere & "([tbxCaseSearchDescription] Like ""*" & Me.tbxDescription & "*"") AND "
  82.     End If
  83.  
  84.     'Number field example. Do not add the extra quotes.
  85.     'If Not IsNull(Me.tbxOfficer_) Then
  86.         'strWhere = strWhere & "([tbxCaseSearchOfficer_] = " & Me.tbxOfficer_ & ") AND "
  87.     'End If
  88.  
  89.     '***********************************************************************
  90.     'Chop off the trailing " AND ", and use the string as the form's Filter.
  91.     '***********************************************************************
  92.     'See if the string has more than 5 characters (a trailng " AND ") to remove.
  93.     lngLen = Len(strWhere) - 5
  94.     If lngLen <= 0 Then     'Nah: there was nothing in the string.
  95.         MsgBox "No criteria", vbInformation, "Nothing to do."
  96.     Else                    'Yep: there is something there, so remove the " AND " at the end.
  97.         strWhere = Left$(strWhere, lngLen)
  98.         'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
  99.         'Debug.Print strWhere
  100.  
  101.         'Finally, apply the string as the form's Filter.
  102.         Me.Filter = strWhere
  103.         Me.FilterOn = True
  104.     End If
  105. End Sub
Dec 26 '06 #3

Expert 5K+
P: 8,434
After days of searching I finally an example that would work with my application, the only problem is after entering all of the code it is not working. Would someone be kind enough to take a look at this...
I think it would help if you could give more speciofic details of the problem. In other words, what precisely does "not working" mean? We need details of any errors which are occurring, and at what line of code, etc.
Dec 26 '06 #4

P: 6
Thankyou to all that have replied.

This has actually been on ongoing problem and I have actually posted here about this before and with the info I provided at the time, Nemo was not able to help.

Applications details:
Access 2003 (file format 2000)
Tables: tblFCR, tblCaseNumber

To make things easier for the end user I have a created a form (frmTabAll) that have four tabbed subforms in it; in the first two subforms (sfmFCR, sfmCaseNumber) I have created another subform (sfmFCRHistory, sfmCaseNumberHistory) that display all the records for the original table. These two forms work perfectly for data entry, when the end user inputs their data the sorted results of for each show up below.

This is where the problem is, the third and fourth subforms that I have created are for searching (sfmFCRSeach, SfmCaseNumberSearch). This is what I am wanting to do -- have unbound text boxes for each field with the results that show up in the second subform (sfmFCRSearchResults, sfmCaseNumberSearchResults) that is below with a cmdSearch_Click command.

I realize that the original code that I have posted is a little different from what I have described but when I was working with it, I was willing to make that sacrefice.

To answer one of the above questions...When I place the above code under the on click for my search button and try to run the search, there are no results displayed.

I have also thought about creating a QBF but for some reason this is not wanting to work in 2003, when I click search all I am getting is the pop-up box asking me the input. Another option is to create a VBA filter, I just lack the knowledge to write the entire code myself.

Here is the link for my original post on this:

Original Post

I hope everyone understands what I am talking about.

Thanks again for all of your help.

Ron

p.s. there is a picture of frmTabAll/sfmCaseNumberSearch in my original post if that helps.
Dec 27 '06 #5

P: 6
A couple of other things to mention if it helps...

When I started with this application I made all of the fields with the (exept the primary key) text fields; this includes the case number, officer #, and date fields. This was done so that I could import the fields from our original DB without any problems, the original was Paradox 3.5. All I have to say is that 65,000 records got imported without a problem. I call this luck, I guess that someone was on my side that day!

I have managed to import all of the records over and I am thinking of leaving these fields as text because through out the years the dispatcher have put in different formats of the dates and such.

The only thing that I can think of that I forgot to mention is that some of the search fields will have null value when searched.
Dec 27 '06 #6

Post your reply

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