Parameters in queries

  • Thread starter Thread starter colmkav
  • Start date Start date
C

colmkav

Hi,

how can I write a query that specifies the value of a parameter that
exists in a subquery.

eg

SELECT field FROM qrySUB
WHERE ....

qrySUB:

SELECT field where fldDate = [addDate]

Basically the reason I want to do this is that I want two similar
queries one that specifically uses todays date and the other where you
can choose the date and I dont want to duplicate all my subqueries for
each.
 
colmkav said:
how can I write a query that specifies the value of a parameter that
exists in a subquery.

eg

SELECT field FROM qrySUB
WHERE ....

qrySUB:

SELECT field where fldDate = [addDate]

Basically the reason I want to do this is that I want two similar
queries one that specifically uses todays date and the other where you
can choose the date and I dont want to duplicate all my subqueries for
each.


Parameters that automatically prompt for criteria like that
are just a quick and dirty approach that is not really a
good way to interact with a running application. Far better
to use a form with a text box for the parameter and a button
to open the form or report based on the query. The button's
Click event procedure can easily check if there is nothing
in the text box and stuff todays date in it:
If IsNull(Me.thetextbox) Then Me.thetextbox = Date
This way, the parameter would be written:
Forms!theform.thetextbox
If you do that, you will often find that your artificial
need for a subquery disappears.

If the form or report can use a record source query without
a parameter in a subquery, then you can totally avoid using
a parameter in the query. In this case the button's click
event would use the OpenForm/Report method's WhereCondition
argument to filter the query's records:

DoCmd.OpenForm "yourform", , , "fldDate=" & _
Format(Me.thetextbox, "\#yyyy-m-d\#")
 
colmkav said:
how can I write a query that specifies the value of a parameter that
exists in a subquery.

SELECT field FROM qrySUB
WHERE ....

SELECT field where fldDate = [addDate]
Basically the reason I want to do this is that I want two similar
queries one that specifically uses todays date and the other where you
can choose the date and I dont want to duplicate all my subqueries for
each.

Parameters that automatically prompt for criteria like that
are just a quick and dirty approach that is not really a
good way to interact with a running application.  Far better
to use a form with a text box for the parameter and a button
to open the form or report based on the query.  The button's
Click event procedure can easily check if there is nothing
in the text box and stuff todays date in it:
        If IsNull(Me.thetextbox) Then Me.thetextbox = Date
This way, the parameter would be written:
        Forms!theform.thetextbox
If you do that, you will often find that your artificial
need for a subquery disappears.

If the form or report can use a record source query without
a parameter in a subquery, then you can totally avoid using
a parameter in the query.  In this case the button's click
event would use the OpenForm/Report method's WhereCondition
argument to filter the query's records:

DoCmd.OpenForm "yourform", , , "fldDate=" & _
                                                                                Format(Me.thetextbox, "\#yyyy-m-d\#")

--
Marsh
MVP [MS Access]- Hide quoted text -

- Show quoted text -

thanks for this suggestion. But is there also a way to do it one
query? Something like Set parameter = today() or something like that?
 
colmkav said:
colmkav said:
how can I write a query that specifies the value of a parameter that
exists in a subquery.

SELECT field FROM qrySUB
WHERE ....

SELECT field where fldDate = [addDate]
Basically the reason I want to do this is that I want two similar
queries one that specifically uses todays date and the other where you
can choose the date and I dont want to duplicate all my subqueries for
each.

Parameters that automatically prompt for criteria like that
are just a quick and dirty approach that is not really a
good way to interact with a running application.  Far better
to use a form with a text box for the parameter and a button
to open the form or report based on the query.  The button's
Click event procedure can easily check if there is nothing
in the text box and stuff todays date in it:
        If IsNull(Me.thetextbox) Then Me.thetextbox = Date
This way, the parameter would be written:
        Forms!theform.thetextbox
If you do that, you will often find that your artificial
need for a subquery disappears.

If the form or report can use a record source query without
a parameter in a subquery, then you can totally avoid using
a parameter in the query.  In this case the button's click
event would use the OpenForm/Report method's WhereCondition
argument to filter the query's records:

DoCmd.OpenForm "yourform", , , "fldDate=" & _
                  Format(Me.thetextbox, "\#yyyy-m-d\#")

thanks for this suggestion. But is there also a way to do it one
query? Something like Set parameter = today() or something like that?


Depends on what you are doing with the query.

I described how to use a parameter when the query is used as
the record source for a form or report.

If you are opening a recordset on the query, then you can
use the query's Parameters collection:

Dim db As Database
Dim qdf As QueryDef
Dim rs As DAO Recordset
Set db = CurrentDb
Set qdf = db.QueryDefs!thequery
qdf.Parameters!addDate = Date
Set rs = qdf.OpenRecordset(dbOpenDynaset)
. . .
rs.Close: Set rs = Nothing
Set qdf = Nothing
Set db = Nothing

In the later versions of Access where forms have the
Recordset property, you can assign the open recordset to the
form's recordset:

Me.Recordset = rs
 
There are two ways you might use subqueries.

If your subquery returns a single value, you can just use it as criteria.

Such as:

SELECT tblEmployees.LastName, tblEmployees.FirstName,
tblEmployees.MiddleInitial
FROM tblEmployees
WHERE (((tblEmployees.EmployeeCategoryID)=(select EmployeeCategoryID from
tblEmployeeCategories where EmployeeCategoryDescription = 'Shop')));

If your subquery returns a list, the list should return only one column of
data. In this case use the IN clause in the criteria like so:

SELECT tblEmployees.LastName, tblEmployees.FirstName,
tblEmployees.MiddleInitial
FROM tblEmployees
WHERE (((tblEmployees.EmployeeCategoryID) In (select EmployeeCategoryID from
tblEmployeeCategories where EmployeeCategoryDescription like 'E*')));


Always use the IN clause if you do not know the number of rows the subquery
will return.


Hope this helps!
 
Wow, here I go preaching what a subquery is and that wasn't your question at
all, LOL. Sorry.

After re-reading your post, I have an answer.

If you use a parameter in a query, like [addDate] in your example, you can
use the parameter in as many different places within the query as you would
like (you only get one prompt, not multiple prompts.) So, you could have a
query like this:

SELECT field, [addDate] As ParameterEntered where fldDate = [addDate]

This will allow your topmost query "know" the parameter value of qrySUB by
selecting it:

SELECT field, ParameterEntered FROM qrySUB WHERE ....
 
colmkav said:
colmkav wrote:
how can I write a query that specifies the value of a parameter that
exists in a subquery.
eg
SELECT field FROM qrySUB
WHERE ....
qrySUB:
SELECT field where fldDate = [addDate]
Basically the reason I want to do this is that I want two similar
queries one that specifically uses todays date and the other where you
can choose the date and I dont want to duplicate all my subqueries for
each.
Parameters that automatically prompt for criteria like that
are just a quick and dirty approach that is not really a
good way to interact with a running application.  Far better
to use a form with a text box for the parameter and a button
to open the form or report based on the query.  The button's
Click event procedure can easily check if there is nothing
in the text box and stuff todays date in it:
        If IsNull(Me.thetextbox) Then Me.thetextbox = Date
This way, the parameter would be written:
        Forms!theform.thetextbox
If you do that, you will often find that your artificial
need for a subquery disappears.
If the form or report can use a record source query without
a parameter in a subquery, then you can totally avoid using
a parameter in the query.  In this case the button's click
event would use the OpenForm/Report method's WhereCondition
argument to filter the query's records:
DoCmd.OpenForm "yourform", , , "fldDate=" & _
                  Format(Me.thetextbox, "\#yyyy-m-d\#")
thanks for this suggestion. But is there also a way to do it one
query? Something like Set parameter = today() or something like that?

Depends on what you are doing with the query.

I described how to use a parameter when the query is used as
the record source for a form or report.

If you are opening a recordset on the query, then you can
use the query's Parameters collection:

Dim db As Database
Dim qdf As QueryDef
Dim rs As DAO Recordset
Set db = CurrentDb
Set qdf = db.QueryDefs!thequery
qdf.Parameters!addDate = Date
Set rs = qdf.OpenRecordset(dbOpenDynaset)
        . . .
rs.Close: Set rs = Nothing
Set qdf = Nothing
Set db = Nothing

In the later versions of Access where forms have the
Recordset property, you can assign the open recordset to the
form's recordset:

Me.Recordset = rs

--
Marsh
MVP [MS Access]- Hide quoted text -

- Show quoted text -

Sorry but this is VBA. I need it in done in a query.
 
Im not 100% sure what you mean.

here are my exact queries:

INSERT INTO tblBBIDRiskMeasureLink ( BBIdentifier, BBRiskMeasure )
SELECT BBIdentifier, "CREDITDELTA"
FROM QryBitMapRiskMeasuresCreditSEL
WHERE BBIdentifier Not In (SELECT BBIdentifier
FROM tblBBIDRiskMeasureLink
WHERE BBRiskMeasure="CREDITDELTA" );

QryBitMapRiskMeasuresCreditSEL=
SELECT tblSecurityInfo.BBIdentifier, [#addDate] as addDate
FROM (tblProduct INNER JOIN tblDomain ON tblProduct.ProductName =
tblDomain.Code) INNER JOIN tblSecurityInfo ON tblDomain.DomainId =
tblSecurityInfo.ProductCodeId
WHERE (tblSecurityInfo.AddDate>[#addDate] AND
tblProduct.VaRSplitCreditSpreadLong<>"NA");

how do I change these queries so that I can have one query that does
what it does at the moment ie select on date according to that given
by the user and a second query that uses today as the date?




in my example
Wow, here I go preaching what a subquery is and that wasn't your question at
all, LOL.  Sorry.

After re-reading your post, I have an answer.

If you use a parameter in a query, like [addDate] in your example, you can
use the parameter in as many different places within the query as you would
like (you only get one prompt, not multiple prompts.)  So, you could have a
query like this:

SELECT field, [addDate] As ParameterEntered where fldDate = [addDate]

This will allow your topmost query "know" the parameter value of qrySUB by
selecting it:

SELECT field, ParameterEntered FROM qrySUB WHERE ....



colmkav said:
how can I write a query that specifies the value of a parameter that
exists in a subquery.

SELECT field FROM qrySUB
WHERE ....

SELECT field where fldDate = [addDate]
Basically the reason I want to do this is that I want two similar
queries one that specifically uses todays date and the other where you
can choose the date and I dont want to duplicate all my subqueries for
each.- Hide quoted text -

- Show quoted text -
 
I think I have an idea of what you are saying, but please be more
specific about "uses today as the date".

What are you really wanting the first query to do? Be verbose.



Im not 100% sure what you mean.

here are my exact queries:

INSERT INTO tblBBIDRiskMeasureLink ( BBIdentifier, BBRiskMeasure )
SELECT BBIdentifier, "CREDITDELTA"
FROM QryBitMapRiskMeasuresCreditSEL
WHERE BBIdentifier Not In (SELECT BBIdentifier
FROM tblBBIDRiskMeasureLink
WHERE BBRiskMeasure="CREDITDELTA"  );

QryBitMapRiskMeasuresCreditSEL=
SELECT tblSecurityInfo.BBIdentifier, [#addDate] as addDate
FROM (tblProduct INNER JOIN tblDomain ON tblProduct.ProductName =
tblDomain.Code) INNER JOIN tblSecurityInfo ON tblDomain.DomainId =
tblSecurityInfo.ProductCodeId
WHERE (tblSecurityInfo.AddDate>[#addDate] AND
tblProduct.VaRSplitCreditSpreadLong<>"NA");

how do I change these queries so that I can have one query that does
what it does at the moment ie select on date according to that given
by the user and a second query that uses today as the date?

in my example
Wow, here I go preaching what a subquery is and that wasn't your question at
all, LOL.  Sorry.
After re-reading your post, I have an answer.
If you use a parameter in a query, like [addDate] in your example, you can
use the parameter in as many different places within the query as you would
like (you only get one prompt, not multiple prompts.)  So, you could have a
query like this:
SELECT field, [addDate] As ParameterEntered where fldDate = [addDate]
This will allow your topmost query "know" the parameter value of qrySUB by
selecting it:
SELECT field, ParameterEntered FROM qrySUB WHERE ....
colmkav said:
Hi,
how can I write a query that specifies the value of a parameter that
exists in a subquery.
eg
SELECT field FROM qrySUB
WHERE ....
qrySUB:
SELECT field where fldDate = [addDate]
Basically the reason I want to do this is that I want two similar
queries one that specifically uses todays date and the other where you
can choose the date and I dont want to duplicate all my subqueries for
each.- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
Well the first query should use the date given by the user as a
parameter
the 2nd query should use todays date instead of the parameter. ie
using the date() function.

But Id like not to have to duplicate the first query exactly if
possible. ie both queries should use the same subquery not 2 separate
subqueries

Hope that makes sense

I think I have an idea of what you are saying, but please be more
specific about "uses today as the date".

What are you really wanting the first query to do?  Be verbose.

Im not 100% sure what you mean.
here are my exact queries:
INSERT INTO tblBBIDRiskMeasureLink ( BBIdentifier, BBRiskMeasure )
SELECT BBIdentifier, "CREDITDELTA"
FROM QryBitMapRiskMeasuresCreditSEL
WHERE BBIdentifier Not In (SELECT BBIdentifier
FROM tblBBIDRiskMeasureLink
WHERE BBRiskMeasure="CREDITDELTA"  );
QryBitMapRiskMeasuresCreditSEL=
SELECT tblSecurityInfo.BBIdentifier, [#addDate] as addDate
FROM (tblProduct INNER JOIN tblDomain ON tblProduct.ProductName =
tblDomain.Code) INNER JOIN tblSecurityInfo ON tblDomain.DomainId =
tblSecurityInfo.ProductCodeId
WHERE (tblSecurityInfo.AddDate>[#addDate] AND
tblProduct.VaRSplitCreditSpreadLong<>"NA");
how do I change these queries so that I can have one query that does
what it does at the moment ie select on date according to that given
by the user and a second query that uses today as the date?
in my example
Wow, here I go preaching what a subquery is and that wasn't your question at
all, LOL.  Sorry.
After re-reading your post, I have an answer.
If you use a parameter in a query, like [addDate] in your example, youcan
use the parameter in as many different places within the query as you would
like (you only get one prompt, not multiple prompts.)  So, you couldhave a
query like this:
SELECT field, [addDate] As ParameterEntered where fldDate = [addDate]
This will allow your topmost query "know" the parameter value of qrySUB by
selecting it:
SELECT field, ParameterEntered FROM qrySUB WHERE ....
:
Hi,
how can I write a query that specifies the value of a parameter that
exists in a subquery.
eg
SELECT field FROM qrySUB
WHERE ....
qrySUB:
SELECT field where fldDate = [addDate]
Basically the reason I want to do this is that I want two similar
queries one that specifically uses todays date and the other where you
can choose the date and I dont want to duplicate all my subqueries for
each.- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
Where are you running the query? If you execute it from code, you
could specify the parameter so you could have the 2 queries you need
with only one querydef. It's going to take that to avoid creating the
second query.

In code you could do something like this:

Dim qd As QueryDef

Set qd = CurrentDb.QueryDefs("Query1") ' Your INSERT query

qd.Parameters("#addDate") = Date

qd.Execute

-OR-

qd.Parameters("#addDate") = txtAddDate ' User's input

qd.Execute


Then wrapper this in some type of conditional statement to determine
what it fills the parameter with.




Well the first query should use the date given by the user as a
parameter
the 2nd query should use todays date instead of the parameter. ie
using the date() function.

But Id like not to have to duplicate the first query exactly if
possible. ie both queries should use the same subquery not 2 separate
subqueries

Hope that makes sense

I think I have an idea of what you are saying, but please be more
specific about "uses today as the date".
What are you really wanting the first query to do?  Be verbose.
Im not 100% sure what you mean.
here are my exact queries:
INSERT INTO tblBBIDRiskMeasureLink ( BBIdentifier, BBRiskMeasure )
SELECT BBIdentifier, "CREDITDELTA"
FROM QryBitMapRiskMeasuresCreditSEL
WHERE BBIdentifier Not In (SELECT BBIdentifier
FROM tblBBIDRiskMeasureLink
WHERE BBRiskMeasure="CREDITDELTA"  );
QryBitMapRiskMeasuresCreditSEL=
SELECT tblSecurityInfo.BBIdentifier, [#addDate] as addDate
FROM (tblProduct INNER JOIN tblDomain ON tblProduct.ProductName =
tblDomain.Code) INNER JOIN tblSecurityInfo ON tblDomain.DomainId =
tblSecurityInfo.ProductCodeId
WHERE (tblSecurityInfo.AddDate>[#addDate] AND
tblProduct.VaRSplitCreditSpreadLong<>"NA");
how do I change these queries so that I can have one query that does
what it does at the moment ie select on date according to that given
by the user and a second query that uses today as the date?
in my example
Wow, here I go preaching what a subquery is and that wasn't your question at
all, LOL.  Sorry.
After re-reading your post, I have an answer.
If you use a parameter in a query, like [addDate] in your example, you can
use the parameter in as many different places within the query as you would
like (you only get one prompt, not multiple prompts.)  So, you could have a
query like this:
SELECT field, [addDate] As ParameterEntered where fldDate = [addDate]
This will allow your topmost query "know" the parameter value of qrySUB by
selecting it:
SELECT field, ParameterEntered FROM qrySUB WHERE ....
:
Hi,
how can I write a query that specifies the value of a parameter that
exists in a subquery.
eg
SELECT field FROM qrySUB
WHERE ....
qrySUB:
SELECT field where fldDate = [addDate]
Basically the reason I want to do this is that I want two similar
queries one that specifically uses todays date and the other whereyou
can choose the date and I dont want to duplicate all my subqueriesfor
each.- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
colmkav said:
Sorry but this is VBA. I need it in done in a query.


Not that I ever heard of in a Jet query.

I don't use it, but I think you can do that with an SQL
Server backend db using a pass through query from Access??
 
No. I am not doing it from VBA code (or other code). In that case it
would be very simple.

What I want needs to be in SQL.

Are you saying that its not possible?

regards
Colm


Where are you running the query?  If you execute it from code, you
could specify the parameter so you could have the 2 queries you need
with only one querydef.  It's going to take that to avoid creating the
second query.

In code you could do something like this:

Dim qd As QueryDef

Set qd = CurrentDb.QueryDefs("Query1")   ' Your INSERT query

qd.Parameters("#addDate") = Date

qd.Execute

-OR-

qd.Parameters("#addDate") = txtAddDate   ' User's input

qd.Execute

Then wrapper this in some type of conditional statement to determine
what it fills the parameter with.

Well the first query should use the date given by the user as a
parameter
the 2nd query should use todays date instead of the parameter. ie
using the date() function.
But Id like not to have to duplicate the first query exactly if
possible. ie both queries should use the same subquery not 2 separate
subqueries
Hope that makes sense
I think I have an idea of what you are saying, but please be more
specific about "uses today as the date".
What are you really wanting the first query to do?  Be verbose.
Im not 100% sure what you mean.
here are my exact queries:
INSERT INTO tblBBIDRiskMeasureLink ( BBIdentifier, BBRiskMeasure )
SELECT BBIdentifier, "CREDITDELTA"
FROM QryBitMapRiskMeasuresCreditSEL
WHERE BBIdentifier Not In (SELECT BBIdentifier
FROM tblBBIDRiskMeasureLink
WHERE BBRiskMeasure="CREDITDELTA"  );
QryBitMapRiskMeasuresCreditSEL=
SELECT tblSecurityInfo.BBIdentifier, [#addDate] as addDate
FROM (tblProduct INNER JOIN tblDomain ON tblProduct.ProductName =
tblDomain.Code) INNER JOIN tblSecurityInfo ON tblDomain.DomainId =
tblSecurityInfo.ProductCodeId
WHERE (tblSecurityInfo.AddDate>[#addDate] AND
tblProduct.VaRSplitCreditSpreadLong<>"NA");
how do I change these queries so that I can have one query that does
what it does at the moment ie select on date according to that given
by the user and a second query that uses today as the date?
in my example
Wow, here I go preaching what a subquery is and that wasn't your question at
all, LOL.  Sorry.
After re-reading your post, I have an answer.
If you use a parameter in a query, like [addDate] in your example,you can
use the parameter in as many different places within the query as you would
like (you only get one prompt, not multiple prompts.)  So, you could have a
query like this:
SELECT field, [addDate] As ParameterEntered where fldDate = [addDate]
This will allow your topmost query "know" the parameter value of qrySUB by
selecting it:
SELECT field, ParameterEntered FROM qrySUB WHERE ....
:
Hi,
how can I write a query that specifies the value of a parameter that
exists in a subquery.
eg
SELECT field FROM qrySUB
WHERE ....
qrySUB:
SELECT field where fldDate = [addDate]
Basically the reason I want to do this is that I want two similar
queries one that specifically uses todays date and the other where you
can choose the date and I dont want to duplicate all my subqueries for
each.- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
I believe that is exactly what I am saying, but someone else might be
able to come up with a way. I can't think of any way you could do
what you are saying without code.



No. I am not doing it from VBA code (or other code). In that case it
would be very simple.

What I want needs to be in SQL.

Are you saying that its not possible?

regards
Colm

Where are you running the query?  If you execute it from code, you
could specify the parameter so you could have the 2 queries you need
with only one querydef.  It's going to take that to avoid creating the
second query.
In code you could do something like this:
Dim qd As QueryDef
Set qd = CurrentDb.QueryDefs("Query1")   ' Your INSERT query
qd.Parameters("#addDate") = Date


qd.Parameters("#addDate") = txtAddDate   ' User's input

Then wrapper this in some type of conditional statement to determine
what it fills the parameter with.
Well the first query should use the date given by the user as a
parameter
the 2nd query should use todays date instead of the parameter. ie
using the date() function.
But Id like not to have to duplicate the first query exactly if
possible. ie both queries should use the same subquery not 2 separate
subqueries
Hope that makes sense
I think I have an idea of what you are saying, but please be more
specific about "uses today as the date".
What are you really wanting the first query to do?  Be verbose.
Im not 100% sure what you mean.
here are my exact queries:
INSERT INTO tblBBIDRiskMeasureLink ( BBIdentifier, BBRiskMeasure )
SELECT BBIdentifier, "CREDITDELTA"
FROM QryBitMapRiskMeasuresCreditSEL
WHERE BBIdentifier Not In (SELECT BBIdentifier
FROM tblBBIDRiskMeasureLink
WHERE BBRiskMeasure="CREDITDELTA"  );
QryBitMapRiskMeasuresCreditSEL=
SELECT tblSecurityInfo.BBIdentifier, [#addDate] as addDate
FROM (tblProduct INNER JOIN tblDomain ON tblProduct.ProductName =
tblDomain.Code) INNER JOIN tblSecurityInfo ON tblDomain.DomainId =
tblSecurityInfo.ProductCodeId
WHERE (tblSecurityInfo.AddDate>[#addDate] AND
tblProduct.VaRSplitCreditSpreadLong<>"NA");
how do I change these queries so that I can have one query that does
what it does at the moment ie select on date according to that given
by the user and a second query that uses today as the date?
in my example
Wow, here I go preaching what a subquery is and that wasn't yourquestion at
all, LOL.  Sorry.
After re-reading your post, I have an answer.
If you use a parameter in a query, like [addDate] in your example, you can
use the parameter in as many different places within the query as you would
like (you only get one prompt, not multiple prompts.)  So, youcould have a
query like this:
SELECT field, [addDate] As ParameterEntered where fldDate = [addDate]
This will allow your topmost query "know" the parameter value ofqrySUB by
selecting it:
SELECT field, ParameterEntered FROM qrySUB WHERE ....
:
Hi,
how can I write a query that specifies the value of a parameter that
exists in a subquery.
eg
SELECT field FROM qrySUB
WHERE ....
qrySUB:
SELECT field where fldDate = [addDate]
Basically the reason I want to do this is that I want two similar
queries one that specifically uses todays date and the other where you
can choose the date and I dont want to duplicate all my subqueries for
each.- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
Back
Top