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

advice on setting parameters

jim
Is anyone able to provide me with a link to useful documentation or
just outright explain to me how to set query parameters dynamically?

I'm really new to Access and databases in general but I suspect that
there is a way to insert criteria into a query besides opening it up
and typing "and" and "or" on the criteria row of each column.

I have a query that shows thousands of records (students in different
classes taking different tests) and I'd like to be able to produce
reports that show just the records for specific tests, and reports that
show records for specific teachers, and perhaps reports for specific
grade levels, and perhaps look at the data in other unforeseen ways.
How can I dynamically select criteria to sort this query?

Do I have to make a new query or change the query manually each time I
want to run a different sort of report?

Thanks for the advice.

Jim.

Oct 13 '06 #1
6 1683
The simplest solution would be to leave the criteria out of the query, and
build the WhereCondition for OpenReport so it filters the way you want.

There's an example of how to build the criteria string dynamically from only
those boxes where the user actually typed something, in this article:
Search form - Handle many optional criteria
at:
http://allenbrowne.com/ser-62.html
The downloadable example actually sets the Filter for the form, but it is
exactly the same process to set the WhereCondition for OpenReport.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"jim" <0.***********@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Is anyone able to provide me with a link to useful documentation or
just outright explain to me how to set query parameters dynamically?

I'm really new to Access and databases in general but I suspect that
there is a way to insert criteria into a query besides opening it up
and typing "and" and "or" on the criteria row of each column.

I have a query that shows thousands of records (students in different
classes taking different tests) and I'd like to be able to produce
reports that show just the records for specific tests, and reports that
show records for specific teachers, and perhaps reports for specific
grade levels, and perhaps look at the data in other unforeseen ways.
How can I dynamically select criteria to sort this query?

Do I have to make a new query or change the query manually each time I
want to run a different sort of report?

Thanks for the advice.

Jim.

Oct 13 '06 #2
jim
That looks like the right idea but in your documentation you mention
that using the form on a query is a bad idea.

In my case the data I want to filter has been assembled by a query from
multiple tables.
Also I am ultimately interested in printing reports of filtered data
and I'm not sure how to do that if the filtered results are being
displayed within the form.

Jim.

Allen Browne wrote:
The simplest solution would be to leave the criteria out of the query, and
build the WhereCondition for OpenReport so it filters the way you want.

There's an example of how to build the criteria string dynamically from only
those boxes where the user actually typed something, in this article:
Search form - Handle many optional criteria
at:
http://allenbrowne.com/ser-62.html
The downloadable example actually sets the Filter for the form, but it is
exactly the same process to set the WhereCondition for OpenReport.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"jim" <0.***********@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Is anyone able to provide me with a link to useful documentation or
just outright explain to me how to set query parameters dynamically?

I'm really new to Access and databases in general but I suspect that
there is a way to insert criteria into a query besides opening it up
and typing "and" and "or" on the criteria row of each column.

I have a query that shows thousands of records (students in different
classes taking different tests) and I'd like to be able to produce
reports that show just the records for specific tests, and reports that
show records for specific teachers, and perhaps reports for specific
grade levels, and perhaps look at the data in other unforeseen ways.
How can I dynamically select criteria to sort this query?

Do I have to make a new query or change the query manually each time I
want to run a different sort of report?

Thanks for the advice.

Jim.
Oct 13 '06 #3
You can use the technique with a query.

The article suggests that using dynamic criteria like that is better than
using *parameters* in a query.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"jim" <0.***********@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
That looks like the right idea but in your documentation you mention
that using the form on a query is a bad idea.

In my case the data I want to filter has been assembled by a query from
multiple tables.
Also I am ultimately interested in printing reports of filtered data
and I'm not sure how to do that if the filtered results are being
displayed within the form.

Jim.

Allen Browne wrote:
>The simplest solution would be to leave the criteria out of the query,
and
build the WhereCondition for OpenReport so it filters the way you want.

There's an example of how to build the criteria string dynamically from
only
those boxes where the user actually typed something, in this article:
Search form - Handle many optional criteria
at:
http://allenbrowne.com/ser-62.html
The downloadable example actually sets the Filter for the form, but it is
exactly the same process to set the WhereCondition for OpenReport.

"jim" <0.***********@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegr oups.com...
Is anyone able to provide me with a link to useful documentation or
just outright explain to me how to set query parameters dynamically?

I'm really new to Access and databases in general but I suspect that
there is a way to insert criteria into a query besides opening it up
and typing "and" and "or" on the criteria row of each column.

I have a query that shows thousands of records (students in different
classes taking different tests) and I'd like to be able to produce
reports that show just the records for specific tests, and reports that
show records for specific teachers, and perhaps reports for specific
grade levels, and perhaps look at the data in other unforeseen ways.
How can I dynamically select criteria to sort this query?

Do I have to make a new query or change the query manually each time I
want to run a different sort of report?

Oct 13 '06 #4
As you are finding out, putting forms! expression in queries can get really
messy real fast.

Even worse, is now that the query is now "married" and attached to that ONE
form. Often, I have a nice query that I could use MANY times for different
reports, and often even that same query could be used for reports...but then
someone comes along and puts in a expression that means the query is ONLY
good when that form is opened.

Worse, is very hard to control things like having 5 combo boxes, but the
user only selects restrictions in 3 of the combo boxes...and wants the other
2 to be ignore.

I could probably write another 10 or pages as to why putting forms
expressions in queries is bad (besides...it makes the queries real ugly, and
hard to read. and, the sql then is not standard anymore (it will not work
with server based systems either).

So, the solution use now is simply to take the values from the form, and
build your own where clause in code. That way, you simply design the reports
(or forms), and attached them to the query, BUT NO FORMS! conditions are
placed in the query.

To "send" the conditions to the report (or form), you simply use the "where"
clause. This is exactly why ms-access has this feature...and it solves a
zillion problems...and will reduce your development costs by a substantial
amount.

Take a look at the following screen shots to see what I mean:

http://www.members.shaw.ca/AlbertKal.../ridesrpt.html

The code to make those above screens work and launch the report with the
selected restrictions when you hit the "print" button is easy:
dim strWhere as string

' select sales rep combo

if isnull(cboSalesRep) = false then

strWhere = "SalesRep = '" & cboSalesRep & "'"

end if

' select what City for the report

if isnull(cboCity) = false then
if strWhere <"" then
strWhere = strWhere " and "
endif
strWhere = strWhere & "City = '" & cobCity & "'"
end if

Note how the 2nd combo test is setup. You can add as "many" more conditions
you want. Lets say we have a check box to only include Special Customers. We
can add to our very nice prompt screen a check box to

[x] Show Only Special customers

The code we add would be:

if chkSpeicalOnly = True then
if strWhere <"" then
strWhere = strWhere " and "
endif
strWhere = strWhere & "SpecialCust = true"
endif

For sure, each combo and control we add to the nice report screen takes a
bit of code, but no more messy then the query builder..and this way, each
query is nice and clean, and free of a bunch of HIGHLY un-maintainable
forms! expressions.

Further, it means you can re-use the same query for different reports, and
have no worries about some form that is supposed to be open. So, a tiny bit
more code eliminates the messy query problem.. For me, this is very worth
while trade.
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com
Oct 13 '06 #5
Suggestion for much cleaner code:

When there are more combo's involved I often start with:
Dim strWhere as string
strWhere="1=1"
'This is to get rid of the *repeating* syntax if strWhere <"" then ....

'So I add the criteria for the not-empty-combo's simply with
If Not IsNull(Me!cbotxtField) then strWhere=strWhere & " AND txtField = '" & Me!cbotxtField & "'"
If Not IsNull(Me!cboNumField) then strWhere=strWhere & " AND NumField = " & Me!cboNumField
---
---
Docmd.OpenReport "ReportName" , , , strWhere

Arno R
"Albert D. Kallal" <Pl*******************@msn.comschreef in bericht news:FEQXg.134557$5R2.8926@pd7urf3no...
As you are finding out, putting forms! expression in queries can get really
messy real fast.

Even worse, is now that the query is now "married" and attached to that ONE
form. Often, I have a nice query that I could use MANY times for different
reports, and often even that same query could be used for reports...but then
someone comes along and puts in a expression that means the query is ONLY
good when that form is opened.

Worse, is very hard to control things like having 5 combo boxes, but the
user only selects restrictions in 3 of the combo boxes...and wants the other
2 to be ignore.

I could probably write another 10 or pages as to why putting forms
expressions in queries is bad (besides...it makes the queries real ugly, and
hard to read. and, the sql then is not standard anymore (it will not work
with server based systems either).

So, the solution use now is simply to take the values from the form, and
build your own where clause in code. That way, you simply design the reports
(or forms), and attached them to the query, BUT NO FORMS! conditions are
placed in the query.

To "send" the conditions to the report (or form), you simply use the "where"
clause. This is exactly why ms-access has this feature...and it solves a
zillion problems...and will reduce your development costs by a substantial
amount.

Take a look at the following screen shots to see what I mean:

http://www.members.shaw.ca/AlbertKal.../ridesrpt.html

The code to make those above screens work and launch the report with the
selected restrictions when you hit the "print" button is easy:
dim strWhere as string

' select sales rep combo

if isnull(cboSalesRep) = false then

strWhere = "SalesRep = '" & cboSalesRep & "'"

end if

' select what City for the report

if isnull(cboCity) = false then
if strWhere <"" then
strWhere = strWhere " and "
endif
strWhere = strWhere & "City = '" & cobCity & "'"
end if

Note how the 2nd combo test is setup. You can add as "many" more conditions
you want. Lets say we have a check box to only include Special Customers. We
can add to our very nice prompt screen a check box to

[x] Show Only Special customers

The code we add would be:

if chkSpeicalOnly = True then
if strWhere <"" then
strWhere = strWhere " and "
endif
strWhere = strWhere & "SpecialCust = true"
endif

For sure, each combo and control we add to the nice report screen takes a
bit of code, but no more messy then the query builder..and this way, each
query is nice and clean, and free of a bunch of HIGHLY un-maintainable
forms! expressions.

Further, it means you can re-use the same query for different reports, and
have no worries about some form that is supposed to be open. So, a tiny bit
more code eliminates the messy query problem.. For me, this is very worth
while trade.
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com

Oct 13 '06 #6
Instead of "1=1" you could write "True", right? And yes, it is my
observation too that both

expression OR false

and

expression AND true

are equivalent to the expression. Which means that when composing a
conjunct (several AND clauses) or a disjunct (several OR clauses), this
simple starting value helps a lot.

Arno R schreef:
Suggestion for much cleaner code:

When there are more combo's involved I often start with:
Dim strWhere as string
strWhere="1=1"
'This is to get rid of the *repeating* syntax if strWhere <"" then ....
--
Bas Cost Budde
Holland
www.heuveltop.nl/BasCB/msac_index.html
Oct 13 '06 #7

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

Similar topics

3
by: Oasis | last post by:
Hello, I'm new to c#. I have situation where I want to execute a number of insert statements that differ only in a few dynamic values. When I was a Java programmer, I would do this with a...
0
by: Rhino | last post by:
I am working with SQL Functions in DB2 for Windows/Linux/UNIX (V8.2.1) and am having a problem setting input parameters for SQL Functions to null in the Development Center. My simple function,...
2
by: John Baker | last post by:
Hi: I have PDF995 installed (and registered) and contacted the PDF995 tech support for help in setting up to automatically print Access reports using PDF 995. As a result I received the...
3
by: josema | last post by:
Hi to all What is the best way call methods with parameters or create properties to store values? Thanks in advance. Josema.
24
by: Joseph Geretz | last post by:
Up to this point, our application has been using Windows File Sharing to transfer files to and from our application document repository. This approach does not lend itself toward a secure...
1
by: Niggy | last post by:
When setting parameters, I get the error: System.IndexOutOfRangeException: Invalid index -1 With Me.MySqlCommand1 .Parameters("@FileName").Value = sFileName .ExecuteNonQuery() End With
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
5
by: dana1 | last post by:
Hello Experts! Does anyone know if there is a way to set the values of query parameters from VBA for a report's recordsource? (i.e., I want to set the values of the parameters and NOT have the...
13
by: terry.holland | last post by:
I have a three tiered CRM application (ASP.Net UI, VB.Net Business Layer & VB.Net Data Access Layer) that consists of number of classes. The application will be deployed to a number of clients. ...
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:
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...
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: 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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.