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

Add Filters to Form

3
Hi All,

I have found many posts about Queries, however I don't seem to be able to find one solves my problem!

I have a large database which I need users to to be able to filter similar to a form filter.

To solve this I have created a form with four Combo boxes.

What I would like to have is that if the user enters none, one or many search options into the combo box the results are true.

At present I have played with "Is Null" however if filter one for example has five results, and then "Is Null" on a second filter is used then it would display all (1000)
What I think I actually need is a query that ignores the question if empty.(preferably access as I am not experienced in other scripts, but willing to give it a go.)

Hope that makes sense!

Anyone's help and time would be grateful.

Andy
Jun 9 '10 #1

✓ answered by NeoPa

Andy, please check out Example Filtering on a Form. It has an Example database attached that you can play with.

Let us know if this helps you solve your issue or whether more assistance is required.

Welcome to Bytes!

7 3307
jimatqsi
1,271 Expert 1GB
Andy,
You don't say, but I assume you are working from a form, you want to filter data dynamically on a form. If that is the case, maybe this example of something I did will help.

I made a form to show shipping information. I put some text boxes and check boxes at the top of the form. Every time one of those objects changes, the After_Update event causes the form to be refiltered. You can see in this code that the FilterForm() function is called whenever one of those boxes is updated.

Look at the code in the FilterForm function. It creates a SQL query string based on the various filter options available to the user. If any of the filter options are not used, they are ignore in the criteria.

Hope that helps.

Jim

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub FilterForm()
  5. Dim strSQL As String
  6.  
  7. Dim strCriteria As String
  8.  
  9. strSQL = "SELECT CDate(nz([dtmDateShip],Date())) AS Shipdate, dtmDateShip, strShipVia, strShipViaServices, strOrderNumber, strShipmentStatus, strInvoiceID, intPackageCount, "
  10. strSQL = strSQL & " dtmTimeStamp, format(dtmTimeStamp,'MM/DD/YY') as PrDate, curShipmentAddon "
  11. strSQL = strSQL & " FROM tbl_OEShipping "
  12.  
  13. strCriteria = " where 1=1 "
  14.  
  15. If Me!chkUPSOnly Then
  16.     strCriteria = strCriteria & " and instr(strShipvia,""" & "UPS" & """)>0"
  17. End If
  18.  
  19. If Me!chkShippedFilter <> Me!chkUnshippedFilter Then
  20.     If Me!chkUnshippedFilter = True Then
  21.         strCriteria = strCriteria & " and NZ(strShipmentStatus)<> """ & "Shipped" & """ "
  22.     End If
  23.  
  24.     If Me!chkShippedFilter = True Then
  25.         strCriteria = strCriteria & " and NZ(strShipmentStatus)= """ & "Shipped" & """ "
  26.     End If
  27.  
  28. End If
  29.  
  30.     If Nz(Me!txtFilterDate, "") <> "" Then
  31.         strCriteria = strCriteria & " and dtmDateShip=#" & Me!txtFilterDate & "# "
  32.     End If
  33.  
  34.     Dim strDate As String
  35.     strDate = Format(Me!txtFilterDatePrinted, "MM/DD/YY")
  36.  
  37.     If Nz(Me!txtFilterDatePrinted, "") <> "" Then
  38.         strCriteria = strCriteria & " and format(dtmTimeStamp,'MM/DD/YY')=""" & strDate & """ "
  39.     End If
  40.  
  41.     DoCmd.Hourglass True
  42.     Me.Form.RecordSource = strSQL & strCriteria & " ORDER BY CDate(nz([dtmDateShip],Date())) DESC , strShipVia, strOrderNumber;"
  43.     Me.Requery
  44.     DoCmd.Hourglass False
  45.  
  46. End Sub
  47.  
  48. Private Sub chkShippedFilter_AfterUpdate()
  49.     FilterForm
  50.  
  51. End Sub
  52.  
  53. Private Sub chkUnshippedFilter_AfterUpdate()
  54.     FilterForm
  55.  
  56.  
  57. End Sub
  58.  
  59. Private Sub Text20_DblClick(Cancel As Integer)
  60.  
  61. End Sub
  62.  
  63. Private Sub chkUPSOnly_AfterUpdate()
  64.     FilterForm
  65. End Sub
  66.  
  67. Private Sub strOrderNumber_DblClick(Cancel As Integer)
  68.     Dim strOrder As String
  69.     strOrder = Me!strOrderNumber
  70.    DoCmd.Close
  71.    Forms!frm_OEShippingSelectCustomer!txtSelectedOrder = strOrder
  72.  '  Forms!frm_OEShippingSelectCustomer.txtSelectedOrder_AfterUpdate
  73.  
  74. End Sub
  75.  
  76. Private Sub txtDateShip_DblClick(Cancel As Integer)
  77.     Me!txtFilterDate = Me!txtDateShip
  78.     FilterForm
  79.  
  80. End Sub
  81.  
  82. Private Sub txtFilterDate_AfterUpdate()
  83.     FilterForm
  84.  
  85. End Sub
  86.  
  87. Private Sub txtFilterDatePrinted_AfterUpdate()
  88.     FilterForm
  89.  
  90. End Sub
  91.  
  92. Private Sub txtPrintDate_DblClick(Cancel As Integer)
  93.     Me!txtFilterDatePrinted = Me!txtPrintDate
  94.     FilterForm
  95. End Sub
  96. Private Sub cmdClose_Click()
  97. On Error GoTo Err_cmdClose_Click
  98.  
  99.  
  100.     DoCmd.Close
  101.  
  102. Exit_cmdClose_Click:
  103.     Exit Sub
  104.  
  105. Err_cmdClose_Click:
  106.     MsgBox Err.Description
  107.     Resume Exit_cmdClose_Click
  108.  
  109. End Sub
  110.  
Jun 10 '10 #2
NeoPa
32,556 Expert Mod 16PB
Andy, please check out Example Filtering on a Form. It has an Example database attached that you can play with.

Let us know if this helps you solve your issue or whether more assistance is required.

Welcome to Bytes!
Jun 10 '10 #3
anma
3
Neopa

Thank you for your help, it is exactly the type of idea I was looking for.

Just didn't have the knowledge.

One thing is your example works with multiples or individuals, however I can not get mine to do the same.

My Event Procedure looks something like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Product_Change()
  3.     Call CheckFilter
  4. End Sub
  5.  
  6. Private Sub Container_Change()
  7.     Call CheckFilter
  8. End Sub
  9.  
  10. Private Sub CheckFilter()
  11.  
  12.     Dim strFilter As String, strOldFilter As String
  13.  
  14.     strOldFilter = Me.Filter
  15.     'Combobox1
  16.     If Me!Product > "" Then
  17.         strFilter = strFilter & _
  18.                     " AND ([Product Type] Like '" & _
  19.                     Me!Product & "*')"
  20.     'Combobox2
  21.     If Me!Container > "" Then
  22.         strFilter = strFilter & _
  23.                     " AND ([Container Number] Like '" & _
  24.                     Me!Container & "*')"
  25.     'Debug.Print ".Filter = '" & strOldFilter & "' - ";
  26.     'Debug.Print "strFilter = '" & strFilter & " '"
  27.     'Tidy up results and apply IF NECESSARY
  28.     If strFilter > "" Then strFilter = Mid(strFilter, 6)
  29.     If strFilter <> strOldFilter Then
  30.         Me.Filter = strFilter
  31.         Me.FilterOn = (strFilter > "")
  32.  
  33.     End If
  34.     End If
  35.     End If
  36. End Sub
  37.  
  38.  
I have had to add extra "End If" as I was prompted to do so by access. Not sure if that is one of the problems.

Thanks for your help,

Andy
Jun 11 '10 #4
jimatqsi
1,271 Expert 1GB
Andy,
You want your first End If to be before the test for ComboBox2. Otherwise you don't filter anything unless you are filtering by Product. Similarly, I would move the 2nd End If to be before the 3rd If test, and the 3rd End If before the 4th If test. I don't think you really intend to nest any of these If tests.

Also, I like to initialize strFilter with "strFilter = "1=1 ". That way I don't have to worry about stripping the "and" of of the first criteria that may or may not have been added to the filter string.

Jim
Jun 11 '10 #5
NeoPa
32,556 Expert Mod 16PB
It looks like you've missed out the underscores (_) from the If lines at line #16 and #21. These should read respectively :
Expand|Select|Wrap|Line Numbers
  1. If Me!Product > "" Then _
  2. 'and
  3. If Me!Container > "" Then _
This ensures the following line is treated as a continuation. In this case a single line If statement requires no End If

Clearly with this fixed you can remove the lines #34 and #35. These currently skew the whole logic completely.
Jun 11 '10 #6
anma
3
Fantastic!

Thank you all so much.

It works perfectly,

Regards,

Andy
Jun 11 '10 #7
NeoPa
32,556 Expert Mod 16PB
It's been a pleasure Andy :)
Jun 11 '10 #8

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

Similar topics

0
by: david liu | last post by:
access 2000 query: here's what i want to do. from an asp page, perform a search on a table in access. i have used sql code in the asp page itself, but i'd rather execute a query in access. i...
5
by: Art | last post by:
Hi, Can anyone point me to an example of how I would execute a query I've created in an Access DB. I've copied the SQL down to my VB.net application and that works fine, but it's ugly. I'd like...
8
by: s_wadhwa | last post by:
SELECT DISTINCTROW "01C" AS dummy, Buildings.BuildingNumber, UCASE(Buildings.BuildingName) AS BuildingName, Buildings.MasterPlanCode, Buildings.UniformBuildingCode,...
4
by: ShastriX | last post by:
Getting a weird error while trying out a query from Access 2003 on a SQL Server 2005 table. Want to compute the amount of leave taken by an emp during the year. Since an emp might be off for...
12
by: zwasdl | last post by:
Hi, I'm using MS Access to query against Oracle DB via ODBC. Is it possible to use HINT in Access? Thanks, Wei
3
by: Idlemind23 | last post by:
Ahoy! I'm hoping for some help on what (I believe) should be a simple task. I have a form with a button. I would like that button to have a caption with a date. That date should be pulled out...
3
by: =?Utf-8?B?bXNjZXJ0aWZpZWQ=?= | last post by:
Has anyone successfully used an Access query from .NET? I am trying to do this and am getting a weird error. .NET calls queries 'stored procedures'. The error I am getting says "Schema could not be...
2
by: k-man | last post by:
Hi: I have an MS Access query for a table called MyTable. One of my fields in the query is a custom field that looks like "MyField: = MyFunction(ID)" where ID is a field in MyTable. I have...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
15
by: OzNet | last post by:
I have a query (with calculated fields) in Access (2007) and the data changes depending on the dates and staff person selected. I need to produce a series of graphs based on the data in this query...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
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,...

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.