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
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
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 - Option Compare Database
-
Option Explicit
-
-
Private Sub FilterForm()
-
Dim strSQL As String
-
-
Dim strCriteria As String
-
-
strSQL = "SELECT CDate(nz([dtmDateShip],Date())) AS Shipdate, dtmDateShip, strShipVia, strShipViaServices, strOrderNumber, strShipmentStatus, strInvoiceID, intPackageCount, "
-
strSQL = strSQL & " dtmTimeStamp, format(dtmTimeStamp,'MM/DD/YY') as PrDate, curShipmentAddon "
-
strSQL = strSQL & " FROM tbl_OEShipping "
-
-
strCriteria = " where 1=1 "
-
-
If Me!chkUPSOnly Then
-
strCriteria = strCriteria & " and instr(strShipvia,""" & "UPS" & """)>0"
-
End If
-
-
If Me!chkShippedFilter <> Me!chkUnshippedFilter Then
-
If Me!chkUnshippedFilter = True Then
-
strCriteria = strCriteria & " and NZ(strShipmentStatus)<> """ & "Shipped" & """ "
-
End If
-
-
If Me!chkShippedFilter = True Then
-
strCriteria = strCriteria & " and NZ(strShipmentStatus)= """ & "Shipped" & """ "
-
End If
-
-
End If
-
-
If Nz(Me!txtFilterDate, "") <> "" Then
-
strCriteria = strCriteria & " and dtmDateShip=#" & Me!txtFilterDate & "# "
-
End If
-
-
Dim strDate As String
-
strDate = Format(Me!txtFilterDatePrinted, "MM/DD/YY")
-
-
If Nz(Me!txtFilterDatePrinted, "") <> "" Then
-
strCriteria = strCriteria & " and format(dtmTimeStamp,'MM/DD/YY')=""" & strDate & """ "
-
End If
-
-
DoCmd.Hourglass True
-
Me.Form.RecordSource = strSQL & strCriteria & " ORDER BY CDate(nz([dtmDateShip],Date())) DESC , strShipVia, strOrderNumber;"
-
Me.Requery
-
DoCmd.Hourglass False
-
-
End Sub
-
-
Private Sub chkShippedFilter_AfterUpdate()
-
FilterForm
-
-
End Sub
-
-
Private Sub chkUnshippedFilter_AfterUpdate()
-
FilterForm
-
-
-
End Sub
-
-
Private Sub Text20_DblClick(Cancel As Integer)
-
-
End Sub
-
-
Private Sub chkUPSOnly_AfterUpdate()
-
FilterForm
-
End Sub
-
-
Private Sub strOrderNumber_DblClick(Cancel As Integer)
-
Dim strOrder As String
-
strOrder = Me!strOrderNumber
-
DoCmd.Close
-
Forms!frm_OEShippingSelectCustomer!txtSelectedOrder = strOrder
-
' Forms!frm_OEShippingSelectCustomer.txtSelectedOrder_AfterUpdate
-
-
End Sub
-
-
Private Sub txtDateShip_DblClick(Cancel As Integer)
-
Me!txtFilterDate = Me!txtDateShip
-
FilterForm
-
-
End Sub
-
-
Private Sub txtFilterDate_AfterUpdate()
-
FilterForm
-
-
End Sub
-
-
Private Sub txtFilterDatePrinted_AfterUpdate()
-
FilterForm
-
-
End Sub
-
-
Private Sub txtPrintDate_DblClick(Cancel As Integer)
-
Me!txtFilterDatePrinted = Me!txtPrintDate
-
FilterForm
-
End Sub
-
Private Sub cmdClose_Click()
-
On Error GoTo Err_cmdClose_Click
-
-
-
DoCmd.Close
-
-
Exit_cmdClose_Click:
-
Exit Sub
-
-
Err_cmdClose_Click:
-
MsgBox Err.Description
-
Resume Exit_cmdClose_Click
-
-
End Sub
-
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!
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: -
-
Private Sub Product_Change()
-
Call CheckFilter
-
End Sub
-
-
Private Sub Container_Change()
-
Call CheckFilter
-
End Sub
-
-
Private Sub CheckFilter()
-
-
Dim strFilter As String, strOldFilter As String
-
-
strOldFilter = Me.Filter
-
'Combobox1
-
If Me!Product > "" Then
-
strFilter = strFilter & _
-
" AND ([Product Type] Like '" & _
-
Me!Product & "*')"
-
'Combobox2
-
If Me!Container > "" Then
-
strFilter = strFilter & _
-
" AND ([Container Number] Like '" & _
-
Me!Container & "*')"
-
'Debug.Print ".Filter = '" & strOldFilter & "' - ";
-
'Debug.Print "strFilter = '" & strFilter & " '"
-
'Tidy up results and apply IF NECESSARY
-
If strFilter > "" Then strFilter = Mid(strFilter, 6)
-
If strFilter <> strOldFilter Then
-
Me.Filter = strFilter
-
Me.FilterOn = (strFilter > "")
-
-
End If
-
End If
-
End If
-
End Sub
-
-
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
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
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 : - If Me!Product > "" Then _
-
'and
-
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.
Fantastic!
Thank you all so much.
It works perfectly,
Regards,
Andy
NeoPa 32,556
Expert Mod 16PB
It's been a pleasure Andy :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
by: s_wadhwa |
last post by:
SELECT DISTINCTROW "01C" AS dummy, Buildings.BuildingNumber,
UCASE(Buildings.BuildingName) AS BuildingName,
Buildings.MasterPlanCode, Buildings.UniformBuildingCode,...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |