473,387 Members | 1,834 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,387 software developers and data experts.

Help with this filter code please...

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
5 2627
PEB
1,418 Expert 1GB
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
1,418 Expert 1GB
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
Killer42
8,435 Expert 8TB
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
Ron S
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
Ron S
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

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

Similar topics

0
by: Gennadiy Tit | last post by:
I have to load 1700 records from DB into tree view (NOT LIST VIEW). I have tried to load it with locking the windows and control and other stuff, but it not givingme the result that I need. It...
0
by: Matrx | last post by:
Can anyone help me create a sql querry with the following clipper/dbase code or help me understand what the hell it means??? If you can help please don't hesitate to contact me at...
2
by: Sara | last post by:
Dear Sir, I want to access to a special group in active directory but with this function I could just see that a special user is exist in active directory or not, I mean I want to see that a user...
5
by: Rated R1 | last post by:
I wrote this before in the NGs, so I am going to paste the responses that I got and see if someone can please help me. Email me and we can set something up as Id even be willing to pay for your...
6
by: Ubi | last post by:
hi i have a problem with System.Data.DataViewRowState. i have a ReadOnly datagrid, a dataView and a dataTable. i'm using the dataView's filter property to filter the data (firstName =...
5
by: Tim::.. | last post by:
Hi can someone please tell me how I change this directory service query so that it searches through each record in the active directory and returns all the accounts! At the moment I can only get...
0
coderthebarbarian
by: coderthebarbarian | last post by:
Using VB 2005. I've got to be able to connect to a server app in both a domain and a workgroup scenario using IIS. The domain code passes Windows Authentication. The workgroup code requires the user...
9
by: TF | last post by:
Hello all, I made a ASP.NET 2.0 site that shows possible "recipes" for paint colors stored in an access dbase. Basically, 1000 colors are stored with specific RGB values in separate columns. A...
0
by: Shani Aulakh | last post by:
Hi All, I have the following class which deletes files older than X number of days and with specific extension. I dont see any error in the code from my point of you. Please, can anyone double...
2
by: necron99 | last post by:
import RuntimeEnvironment as renv import os, sys import win32com.client import getpass OMI = win32com.client.Dispatch("MOVEitAPI.clientObj") OMI.Host = system
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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...

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.