multiple parameter query

G

Guest

i have a query with two parameters which namely the first asks for a month
and the second asks for a year.(Both month and year are in seperate fields,
where month is a lookup column)

some times i need to run a query just based on the year. how can i do to use
the same query or i have to design a second query. Since when i try to leave
the month parameter empty it gives ne no result

Thanks
Malcolm
 
J

John Vinson

i have a query with two parameters which namely the first asks for a month
and the second asks for a year.(Both month and year are in seperate fields,
where month is a lookup column)

some times i need to run a query just based on the year. how can i do to use
the same query or i have to design a second query. Since when i try to leave
the month parameter empty it gives ne no result

Thanks
Malcolm

Could you please open the query in SQL view and post the SQL text
here? It's hard to know what change to make if we can't see what
you're doing!

I'm guessing that a clause such as

OR [parameter] IS NULL

will work, but posting the SQL will make it easy to be specific.

John W. Vinson[MVP]
 
G

Guest

Hi Malcolm
Another suggestion is, look into using Martin Green tips, Customizing Access
Parameter Queries. Here is the link.
http://fontstuff.com/access/acctut08.htm
--
Tim


John Vinson said:
i have a query with two parameters which namely the first asks for a month
and the second asks for a year.(Both month and year are in seperate fields,
where month is a lookup column)

some times i need to run a query just based on the year. how can i do to use
the same query or i have to design a second query. Since when i try to leave
the month parameter empty it gives ne no result

Thanks
Malcolm

Could you please open the query in SQL view and post the SQL text
here? It's hard to know what change to make if we can't see what
you're doing!

I'm guessing that a clause such as

OR [parameter] IS NULL

will work, but posting the SQL will make it easy to be specific.

John W. Vinson[MVP]
 
G

Guest

HI again,

this is the SQL Text :

SELECT [Member Details].[Membership No], [Member Details].Title, [Member
Details].[First Name], [Member Details].Surname, [Member Details].[Address
1], [Member Details].[Address 2], [Member Details].Town, [Member
Details].Postcode, [Months of the year].Months, [Member Details].Year
FROM [Months of the year] INNER JOIN [Member Details] ON [Months of the
year].ID = [Member Details].Month
WHERE ((([Months of the year].Months)=[Please Enter Month]) AND (([Member
Details].Year)=[Please Enter Year]));


John Vinson said:
i have a query with two parameters which namely the first asks for a month
and the second asks for a year.(Both month and year are in seperate fields,
where month is a lookup column)

some times i need to run a query just based on the year. how can i do to use
the same query or i have to design a second query. Since when i try to leave
the month parameter empty it gives ne no result

Thanks
Malcolm

Could you please open the query in SQL view and post the SQL text
here? It's hard to know what change to make if we can't see what
you're doing!

I'm guessing that a clause such as

OR [parameter] IS NULL

will work, but posting the SQL will make it easy to be specific.

John W. Vinson[MVP]
 
J

John Vinson

this is the SQL Text :

I'd suggest changing the name of the field Year - it's a reserved word
like Month or Date, and Access can get confused! But - using brackets
around the questionable names - use:

SELECT [Member Details].[Membership No], [Member Details].Title,
[Member Details].[First Name], [Member Details].Surname,
[Member Details].[Address 1], [Member Details].[Address 2],
[Member Details].Town, [Member Details].Postcode,
[Months of the year].[Months], [Member Details].[Year]
FROM [Months of the year]
INNER JOIN [Member Details]
ON [Months of the year].ID = [Member Details].[Month]
WHERE ((([Months of the year].Months)=
[Please Enter Month] OR [Please Enter Month] IS NULL)
AND (([Member Details].Year)=[Please Enter Year]));


John W. Vinson[MVP]
 
L

lil2009

I am trying to do the same type of query, but the second parameter is ignored
if nothing is entered into the first parameter. Any idea why?

Thank you

John Vinson said:
this is the SQL Text :

I'd suggest changing the name of the field Year - it's a reserved word
like Month or Date, and Access can get confused! But - using brackets
around the questionable names - use:

SELECT [Member Details].[Membership No], [Member Details].Title,
[Member Details].[First Name], [Member Details].Surname,
[Member Details].[Address 1], [Member Details].[Address 2],
[Member Details].Town, [Member Details].Postcode,
[Months of the year].[Months], [Member Details].[Year]
FROM [Months of the year]
INNER JOIN [Member Details]
ON [Months of the year].ID = [Member Details].[Month]
WHERE ((([Months of the year].Months)=
[Please Enter Month] OR [Please Enter Month] IS NULL)
AND (([Member Details].Year)=[Please Enter Year]));


John W. Vinson[MVP]
 
J

John W. Vinson

I am trying to do the same type of query, but the second parameter is ignored
if nothing is entered into the first parameter. Any idea why?

Please post your actual SQL and the relevant fieldnames and datatypes from
your table.
 
L

lil2009

Hi,

Here is the sql statement.

SELECT tblDepts.DeptID, tblDepts.[Department Name], tblUse.Status,
tblUse.Deliverable
FROM tblDepts LEFT JOIN tblUse ON tblDepts.DeptID = tblUse.DeptID
WHERE (((tblDepts.[Department Name])=[Forms]![frmSelect]![txtName]) AND
((tblUse.Deliverable)=[Forms]![frmSelect]![cboDeliverable])) OR
(((([tblDepts].[Department Name]) Like [Forms]![frmSelect]![txtName]) Is
Null));


Right now, if I type the dept name and select the deliverable on the form,
it gives me the correct record. But if I leave the dept name field blan and
select a deliverable, it returns all records.

If I do not select a dept name and I select the deliverable, it doesn't
return any records.

I want to have a list of parameters that can be selected or ignored. If one
is ignored it should run the query for the selected parameters.

Thank you for your help!
 
J

John Spencer

Note the criteria and grouping of the criteria in the query below.

SELECT tblDepts.DeptID, tblDepts.[Department Name], tblUse.Status,
tblUse.Deliverable
FROM tblDepts LEFT JOIN tblUse ON tblDepts.DeptID = tblUse.DeptID
WHERE
(tblDepts.[Department Name]=[Forms]![frmSelect]![txtName] OR
[Forms]![frmSelect]![txtName] is Null)
AND
(tblUse.Deliverable=[Forms]![frmSelect]![cboDeliverable] OR
[Forms]![frmSelect]![cboDeliverable])

Access will reformat this if you switch to query design view and it will get
more complex, but it should work. If you do this with more than a few fields
the query will get too complex to execute.

Also, since you are filtering records on the right side table your LEFT JOIN
is negated - that is you will have to have a record in both tblDepts and
tblUse that have the same DeptID or no record will be returned for tblDepts.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
Hi,

Here is the sql statement.

SELECT tblDepts.DeptID, tblDepts.[Department Name], tblUse.Status,
tblUse.Deliverable
FROM tblDepts LEFT JOIN tblUse ON tblDepts.DeptID = tblUse.DeptID
WHERE (((tblDepts.[Department Name])=[Forms]![frmSelect]![txtName]) AND
((tblUse.Deliverable)=[Forms]![frmSelect]![cboDeliverable])) OR
(((([tblDepts].[Department Name]) Like [Forms]![frmSelect]![txtName]) Is
Null));


Right now, if I type the dept name and select the deliverable on the form,
it gives me the correct record. But if I leave the dept name field blan and
select a deliverable, it returns all records.

If I do not select a dept name and I select the deliverable, it doesn't
return any records.

I want to have a list of parameters that can be selected or ignored. If one
is ignored it should run the query for the selected parameters.

Thank you for your help!

John W. Vinson said:
Please post your actual SQL and the relevant fieldnames and datatypes from
your table.
 
J

John W. Vinson

Hi,

Here is the sql statement.

SELECT tblDepts.DeptID, tblDepts.[Department Name], tblUse.Status,
tblUse.Deliverable
FROM tblDepts LEFT JOIN tblUse ON tblDepts.DeptID = tblUse.DeptID
WHERE (((tblDepts.[Department Name])=[Forms]![frmSelect]![txtName]) AND
((tblUse.Deliverable)=[Forms]![frmSelect]![cboDeliverable])) OR
(((([tblDepts].[Department Name]) Like [Forms]![frmSelect]![txtName]) Is
Null));

That's not at all what I suggested. A LIKE operator crept in from somewhere...
try

SELECT tblDepts.DeptID, tblDepts.[Department Name], tblUse.Status,
tblUse.Deliverable
FROM tblDepts LEFT JOIN tblUse ON tblDepts.DeptID = tblUse.DeptID
WHERE (tblDepts.[Department Name]=[Forms]![frmSelect]![txtName]
OR [Forms]![frmSelect]![txtName] IS NULL)
AND
(tblUse.Deliverable=[Forms]![frmSelect]![cboDeliverable]
OR [Forms]![frmSelect]![cboDeliverable] IS NULL);

What I'm doing here is using a parenthesized clause for each field, consisting
of two subclauses: one comparing the field to the form control, and the other
comparing the form control to NULL.
Right now, if I type the dept name and select the deliverable on the form,
it gives me the correct record. But if I leave the dept name field blan and
select a deliverable, it returns all records.

It's tricky getting the parenthesis nesting and the ANDs and ORs correct!
If I do not select a dept name and I select the deliverable, it doesn't
return any records.

I want to have a list of parameters that can be selected or ignored. If one
is ignored it should run the query for the selected parameters.

If the list of parameters exceeds three or four, this technique is going to
get excessively complicated. At the very least you should construct the query
in the SQL window and avoid EVER going into Design view; doing so will cause
Access to completely reshuffle the query with a whole lot of calculated fields
and convoluted logic.

An alternative approach is to have multiple criteria textboxes on a form and a
"Search" button; the button would poll through the controls and actually build
a SQL string from scratch, ignoring NULL controls and concatenating a clause
like

AND [Department Name] = [Forms]![frmSelect]![txtName]

when it finds that txtName contains data.

When the SQL string is complete you would use it as the Recordsource of a form
or report.

THere are "end effects" - the first non-null criterion starts with AND, which
is bad grammar; one trick is to start with a string like

SELECT tblDepts.DeptID, tblDepts.[Department Name], tblUse.Status,
tblUse.Deliverable
FROM tblDepts LEFT JOIN tblUse ON tblDepts.DeptID = tblUse.DeptID
WHERE 1 = 1

which will retrieve all records; appending one or more AND clauses to this
will restrict the retrieval.
 
J

James A. Fortune

lil2009 said:
Hi,

Here is the sql statement.

SELECT tblDepts.DeptID, tblDepts.[Department Name], tblUse.Status,
tblUse.Deliverable
FROM tblDepts LEFT JOIN tblUse ON tblDepts.DeptID = tblUse.DeptID
WHERE (((tblDepts.[Department Name])=[Forms]![frmSelect]![txtName]) AND
((tblUse.Deliverable)=[Forms]![frmSelect]![cboDeliverable])) OR
(((([tblDepts].[Department Name]) Like [Forms]![frmSelect]![txtName]) Is
Null));


Right now, if I type the dept name and select the deliverable on the form,
it gives me the correct record. But if I leave the dept name field blan and
select a deliverable, it returns all records.

If I do not select a dept name and I select the deliverable, it doesn't
return any records.

I want to have a list of parameters that can be selected or ignored. If one
is ignored it should run the query for the selected parameters.

Thank you for your help!

Maybe:

PARAMETERS Forms!frmSelect!txtName.Value Text,
Forms!frmSelect!cboDeliverable.Value Text;
SELECT tblDepts.DeptID, tblDepts.[Department Name], tblUse.Status,
tblUse.Deliverable
FROM tblDepts LEFT JOIN tblUse ON tblDepts.DeptID = tblUse.DeptID
WHERE IIf(Forms!frmSelect!txtName.Value = "", True, tblDepts.[Department
Name] = Forms!frmSelect!txtName.Value) AND
IIf(Forms!frmSelect!cboDeliverable.Value = "", True,
tblDepts.[Department Name] = Forms!frmSelect!cboDeliverable.Value);

That way you don't have to change the SQL string dynamically because the
controls containing blanks will always return "True" from their
respective IIf() functions, which in a series of logical AND's will, in
effect, cause the blanks to be ignored. It even eliminates the small
problem of making sure the WHERE clause doesn't start with 'AND'.

James A. Fortune
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top