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

Using flexible query criteria

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 on a query. The
criteria for the query is based on the user's input in the main form.
One of the user inputs is a field called "Vendor_Name" (in the form of
a combo box). I would like the query to run for all "Vendor_Name" if
the user leaves the field blank. However, if the user selects a
"Vendor_Name", then I would like the query to run with the selected
criteria.

I've tried an "IIF" statement in the query criteria with no luck, also
tried (my best) to write VB code to do the same, again without any
luck.

I'm sure there is a simple solution, but I have yet to find it. Any
help at all would be appreciated.

Best,

Kelii

Mar 17 '06 #1
7 3760
> I have a form with a subform. The subform is based on a query. The
criteria for the query is based on the user's input in the main form.
One of the user inputs is a field called "Vendor_Name" (in the form of
a combo box). I would like the query to run for all "Vendor_Name" if
the user leaves the field blank. However, if the user selects a
"Vendor_Name", then I would like the query to run with the selected
criteria.


I suggest you to change the SELECT used for RowSource of the combo in the
main form.

Said the SELECT statement is similar to

SELECT Filed1, Field2, ...., FieldN FROM MyTable

and said that Field1 is the primary key and is an autonumber field, and Field2
is the field shown in the combo box, you must modify it to a UNION select,
similar to

SELECT Field1, Field2, ...., FieldN FROM MyTable
UNION 0 AS Field1, '<all>' AS Field2, NULL AS Field3, ...., NULL AS FieldN
FROM MyTable

then, you should change the SELECT used for RecordSource of the subform.

Said this SELECT is similar to

SELECT Field1, Field2, ...., FieldX FROM MyTable

you must modify it in a way similar to

SELECT Field1, Field2, ...., FieldX FROM MyTable
WHERE (Field1 = [Forms]![nameofthemainform]![nameofthecombobox]) OR
([Forms]![nameofthemainform]![nameofthecombobox] = 0)

Bye.

--
PBsoft di Gabriele Bertolucci
www.pbsoft.it
skype: pbsoftsolution
Mar 17 '06 #2
Open your underlying query in design-grid view and something like below:

Like IIf([Forms]![frmMyForm]![pickCombBox] Is
Null,"*",[forms]![frmMyForm]![pickCombBox])

as the criteria in the column of your main form combobox.
-Ed

<ke****@yahoo.com> wrote in message
news:11*********************@j52g2000cwj.googlegro ups.com...
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 on a query. The
criteria for the query is based on the user's input in the main form.
One of the user inputs is a field called "Vendor_Name" (in the form of
a combo box). I would like the query to run for all "Vendor_Name" if
the user leaves the field blank. However, if the user selects a
"Vendor_Name", then I would like the query to run with the selected
criteria.

I've tried an "IIF" statement in the query criteria with no luck, also
tried (my best) to write VB code to do the same, again without any
luck.

I'm sure there is a simple solution, but I have yet to find it. Any
help at all would be appreciated.

Best,

Kelii

Mar 17 '06 #3
Ed - thanks for the response, it didn't seem to work.

This is the text that I put into the query criteria:
Like IIf([Forms]![frmInvoiceLookup]![Vendor_Name] Is
Null,"*",[Forms]![frmInvoiceLookup]![Vendor_Name])

Here is the full query:
SELECT tblInvoiceMaster.Invoice_ID, tblInvoiceMaster.Company_Location,
tblInvoiceMaster.Invoice_Date, tblInvoiceMaster.Invoice_Number,
tblInvoiceMaster.Vendor_Name, tblInvoiceMaster.Total_Invoice_Amount
FROM tblInvoiceMaster GROUP BY tblInvoiceMaster.Invoice_ID,
tblInvoiceMaster.Company_Location, tblInvoiceMaster.Invoice_Date,
tblInvoiceMaster.Invoice_Number, tblInvoiceMaster.Vendor_Name,
tblInvoiceMaster.Total_Invoice_Amount HAVING
(((tblInvoiceMaster.Company_Location)=[Forms]![frmInvoiceLookup]![Company_Location])
AND
((tblInvoiceMaster.Invoice_Date)>=[Forms]![frmInvoiceLookup]![Invoice_Begin_Date]
And
(tblInvoiceMaster.Invoice_Date)<=[Forms]![frmInvoiceLookup]![Invoice_End_Date])
AND ((tblInvoiceMaster.Vendor_Name) Like
IIf([Forms]![frmInvoiceLookup]![Vendor_Name] Is
Null,"*",[Forms]![frmInvoiceLookup]![Vendor_Name])));

Any other thoughts?

Best,

Kelii

Mar 17 '06 #4
Hello Kel,

With the risk of sounding redundant, the expression below
is basically what I use to deal with the problem your having
and works for me everytime.

Perhaps you can give it a go! I use the build button to get
the correct path I need to the control on the main form for
the field in the query.

Result:

Forms![MainFormName]![FieldNameOnForm] 0r
Forms![MainFormName]![FieldNameOnForm] Is Null

Just get the path to the control you need and then repeat
it again after an "or" with the "Is Null" at the end.

This way, it looks to see if there is data in the field first,
then if it is null, you get all records.

Regards

Mar 17 '06 #5
It looks OK; try deleting all the other criteria from your query and test
if the response works then. This assume that ALL records have value in
[Vendor_Name]; if not you'll have to add "or is Null" to that particular
query criteria.
-Ed
<ke****@yahoo.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
Ed - thanks for the response, it didn't seem to work.

This is the text that I put into the query criteria:
Like IIf([Forms]![frmInvoiceLookup]![Vendor_Name] Is
Null,"*",[Forms]![frmInvoiceLookup]![Vendor_Name])

Here is the full query:
SELECT tblInvoiceMaster.Invoice_ID, tblInvoiceMaster.Company_Location,
tblInvoiceMaster.Invoice_Date, tblInvoiceMaster.Invoice_Number,
tblInvoiceMaster.Vendor_Name, tblInvoiceMaster.Total_Invoice_Amount
FROM tblInvoiceMaster GROUP BY tblInvoiceMaster.Invoice_ID,
tblInvoiceMaster.Company_Location, tblInvoiceMaster.Invoice_Date,
tblInvoiceMaster.Invoice_Number, tblInvoiceMaster.Vendor_Name,
tblInvoiceMaster.Total_Invoice_Amount HAVING
(((tblInvoiceMaster.Company_Location)=[Forms]![frmInvoiceLookup]![Company_Location])
AND
((tblInvoiceMaster.Invoice_Date)>=[Forms]![frmInvoiceLookup]![Invoice_Begin_Date]
And
(tblInvoiceMaster.Invoice_Date)<=[Forms]![frmInvoiceLookup]![Invoice_End_Date])
AND ((tblInvoiceMaster.Vendor_Name) Like
IIf([Forms]![frmInvoiceLookup]![Vendor_Name] Is
Null,"*",[Forms]![frmInvoiceLookup]![Vendor_Name])));

Any other thoughts?

Best,

Kelii

Mar 17 '06 #6
Inkman,

Thanks for the response, your suggestion works for 99% of the issue,
which I'm willing to live with. The only complication is that the "Is
Null" criteria seems to prevent the other query criteria from
functioning properly; e.g., the date criteria have no effect when I
leave the Vendor_Name criteria blank. I've repeated the date query
criteria in the second row of the query criteria along with the "Is
Null" for Vendor_Name, but still no luck.

I'll keep playing with it to see if it works, however, great
suggestions and thanks for the help.

Best,

Kelii

Mar 20 '06 #7
Inkman,

Scratch earlier comment, your suggestion works perfectly.

Thanks again,

Kelii

Mar 20 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Steve | last post by:
I am using Access 2000 and I have a DB that is currently running four different queries from some Forms and subforms. These queries each have a date field that are seperate from each other that are...
4
waynetheengineer
by: waynetheengineer | last post by:
Hi, I was wondering if anyone had any suggestions on my database: I have a form that accepts user input into a single text box. When the OK button is hit, a query should search for all records...
3
by: ericargent | last post by:
Hi I'm using Acces 2003 I have Query where the several parameters for the criteria are supplied from a form. One parameter source is a combo box. What I am trying to do is if: An item is...
3
by: sfrvn | last post by:
I have searched high and low and cannot find an answer to my problem. So now I turn to the collective genius of this newsgroup. Over-simplified examples This query criteria for field works:...
2
by: cmartin1986 | last post by:
First of all I want to thank all of you that have helped me in the past this is an awesome fourm. My problem today is I have a database that builds charts that are viewed by a large group every...
4
by: tbeers | last post by:
Is there a method to pass along a criteria argument directly to the query criteria rather than through filtering a form or report? In other words, I would like to click a "print" button and in the...
8
by: limperger | last post by:
Hello everyone! First and foremost, my apologies for the title of the post. It is not very clarifying of what the problem is about, but I didn't know how to put it... My problem is as follows: I...
17
by: sharsy | last post by:
Hello guys, I would like some help in generating query criteria that will identify credit cards that have expired on an access database. The specific Field is formatted with a Data Type of...
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. ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.