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

Set Query Criteria based on Control values

63
I have a report based on several criteria that the user can select in listboxes on a form.

Each of these criteria have an "ALL" option which, if selected, will prevent that criteria from being added to the WHERE clause in the Query string.

Currently, this is done in VBA, but I was wondering if there was a way to do this in the SQL statement itself (or the Query designer in Access), so I can rely less on VBA.

Thanks in advance.
Jun 8 '11 #1
3 6151
Mihail
759 512MB
Expand|Select|Wrap|Line Numbers
  1. Dim MyCriteria As String
  2.     MyCriteria = MyListBox
  3.     If MyCriteria = "ALL" Then
  4.         MyCriteria = "*"
  5.     End If
  6.  
  7.     Dim MySQL As String
  8.     MySQL = _
  9.         "SELECT TableName.FieldName " & _
  10.         "FROM TableName " & _
  11.         "WHERE (((TableName.FieldName) Like """ & MyCriteria & """));"
  12.  
Another solution:
Create a PUBLIC Function

Expand|Select|Wrap|Line Numbers
  1. Public Function ReturnCriteria(FieldIdentifier As String) As String
  2. 'The argument FieldIdentifier will pass the FieldName from your query to tha function
  3.     Select Case FieldIdentifier
  4.         Case "Field_1_Name"
  5.             ReturnCriteria = ListBox_1
  6.         Case "Field_2_Name"
  7.             ReturnCriteria = ListBox_2
  8.         '.............................
  9.     End Select
  10.     If ReturnCriteria = "ALL" Then
  11.         ReturnCriteria = "Like ""*"""
  12.     End If
  13. End Function
In your query, in "Criteria" row, for every field where you wish to pass criteria from a list box, write:
ReturnCriteria("FieldName")


Good luck !
Jun 9 '11 #2
Adam Tippelt
137 100+
I've recently done this myself for a report based on a union query that references about 8 different tables from 7 different filter boxes. I used combo boxes as opposed to list boxes, but I'll talk you through what I did as I don't seem why listboxes would be a problem:

Firstly, how have you added in the All option? Have you added it as another value in the table, or just added it to the filter using SQL?
I found the SQL route is tidier because the all option was only needed to indicate 'no filter', and not an actual value in the table, so for the rowsource of each filter listbox, you're going to want something along the lines of:

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT TableName.FieldName FROM TableName UNION SELECT '(All)' FROM TableName
  2. ORDER BY TableName.FieldName;
(Putting brackets around All defaults it to the top of the list)

After I did that for each filter box, the report recordsource can be based on a query like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT TableName.Field1, TableName.Field2, TableName.Field3
  2. FROM TableName 
  3. WHERE (((TableName.Field1=Forms! FilterForm!Filter1 Or Forms! FilterForm!Filter1 ="(All)")=True) And ((TableName.Field2=Forms!FilterForm!Filter2 Or Forms! FilterForm!Filter2="(All)")=True) And ((TableName.Field3=Forms! FilterForm!Filter3 Or Forms! FilterForm!Filter3 Is Null)=True))
  4. ORDER BY Whatever;
  5.  
This example treats Field1 and Field2 as strings, and Field3 as an integer.

As you've probably noticed, there is slightly different coding for field3 - this is because integer fields have a slight problem with the '(All)' value - because '(All)' is a string, when you try and run the report it errors as a type mismatch. I don't know whether changing the field to type Variant would solve this, but to get round it, on the "load report" button on the filter form, I added some code before it opened the report:

Expand|Select|Wrap|Line Numbers
  1. If Me.IntegerField.Value = "(All)" Then
  2.     Me.IntegerField.Value = Null
  3. End If
  4. DoCmd.OpenReport "ReportName", acViewReport
This will allow you to create an entire query to base the report on, with only a very small amount of programming required to handle integers (maybe not even required if Variant works)

It might not be the perfect solution, but it's one I've found to work well.

Hope that helps.

Adam.
Jun 9 '11 #3
postman
63
Thank you both for your responses. That will help.

Thanks.
Jun 9 '11 #4

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

Similar topics

2
by: neptune | last post by:
I have a query where each customer has an or . Sometimes both fields for a customer are populated, but if is null, then will be populated and vice versa. I have a form, , where I select a...
1
by: rc | last post by:
reposting from comp.database.ms-access "rc" <robert.cipriani@us.army.mil> wrote in message news:ayLtc.27697$0X2.969222@twister.tampabay.rr.com... > I have a table with a single record that...
7
by: keliie | last post by:
Hello Just a quick question that I've spent a few hours trying to solve with no luck (although one would think this should be fairly easy). I have a form with a subform. The subform is based...
7
by: Triple7 | last post by:
Hi there, basically what I am trying to do (without using code as I don’t know how to do that with a code) is to retrieve the previous 5 days & the next 5 days (date wise)against a query1 that...
5
by: jslssuze | last post by:
I have medium experience with Access but need help trying to create a query for the following scenario... any and all help will be greatly appreciated! A user will select different items on an...
2
by: shreyasi | last post by:
i create a page..there i use check box..how can i call perticular quary from mysql using the check box.i want if i select a checkbox and submit the form then the corrosponding record will be...
1
by: pipestew | last post by:
I have a form which allows me to enter a value and then view a report based on that value. The value is currently entered as P-##### and the query criteria is... !! I want the Form to have...
9
ajhayes
by: ajhayes | last post by:
Hello everyone, This is my first time posting here and I'm hoping someone can help me out. I'm a relative newbie to Access and am pretty much learning as I go along, so please bear with me. ...
5
by: hbaf208 | last post by:
I have an option group (TimeFrameOption) with three options: All, Last 6 months, and Last 12 Months. When an option is selected, I want to query dates in a listbox (CallReport) to display all dates...
2
by: Phil Dell | last post by:
Hi all, Extremely new to Access and VBA, I have been working my way through several books and sites, and I'm finally breaking down and crying for help. I am developing a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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...

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.