473,508 Members | 4,179 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optional Parameter for Querydef with Default Built In

I am trying to create a query that can be loaded as a querydef object
but not having to assign values to the parameters if I don't want to.

Normally when using a parameter query in VBA my code would go something
like this:

dim qry as dao.querydef
set qry = currentdb.querydefs("myquery")
qry.parameters("Par1") = "blah"

What I would like to do is build into the query a value that would be
used if I don't manually set the value of the parameter. In this case,
Par1 would be set to "blahblah" unless I specifically set the parameter
to "blah". I know I can somewhat kludge this using a form but I would
like to keep my queries as self-sufficient as possible for reuse in
other forms/reports/modules when the query is called in those places.

This is the criteria I already set for this:
like iif(isnull([Par1]),"*",[Par1])

In this case, if I do not enter anything into the parameter, or I put in
the asterisk I get all the records, while if I put in the full text of
the parameter I get only the record matching that parameter. However,
this still requires me to set the parameter, either in code or manually
when I run the query from the query gui.

A workaround, which in this case is simple, but goes against the grain
anyway, looks like this:

Public Function setQueryDefaults(qryName As String) As DAO.QueryDef
Dim qry As DAO.QueryDef
Set qry = CurrentDb.QueryDefs(qryName)
Dim i As Integer
For i = 0 To qry.Parameters.Count - 1
qry.Parameters(i) = Null
Next i
Set setQueryDefaults = qry
End Function

Of course it can be expanded to handle querydef object initialization
parameters (e.g., dbOpenDynaset), but this would just add to the
overhead to the program. Also, it assumes that the parameters on the
queries are properly set to accept null values.

Of course migrating from such this setup to one in which default
parameters are supported in the desired fashion would be somewhat of a
nightmare, assuming I rewrite the queries for the next database engine.

Marc
Nov 16 '05 #1
21 5717
You can do something like this ---
dim qry as dao.querydef
dim param as parameter
dim SQLStrBlah As String
dim SQLStrBlahBlah As String
set SQLStrBlah = "Select ..... Where..."
set SQLStrBlahBlah = "Select ..... Where..."
set qry = currentdb.querydefs("myquery")
If <Whatever > Then
qry.sql = SQLStrBlah
Else
qry.sql = SQLStrBlah
End If
For each param in qry.parameters
param.value = EVAL(param.name)
Net param
--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
re******@pcdatasheet.com
www.pcdatasheet.com

If you can't get the help you need in the newsgroup, I can help you for a
very reasonable fee. Over 1000 Access users have come to me for help.
Need a month calendar or 7 day calendar? Need appointment scheduling? Need
room reservations scheduling? Need employee work scheduling? Contact me!
"Marc DVer" <ma**@not.in.use> wrote in message
news:Xs*******************@fe10.lga...
I am trying to create a query that can be loaded as a querydef object but
not having to assign values to the parameters if I don't want to.

Normally when using a parameter query in VBA my code would go something
like this:

dim qry as dao.querydef
set qry = currentdb.querydefs("myquery")
qry.parameters("Par1") = "blah"

What I would like to do is build into the query a value that would be used
if I don't manually set the value of the parameter. In this case, Par1
would be set to "blahblah" unless I specifically set the parameter to
"blah". I know I can somewhat kludge this using a form but I would like
to keep my queries as self-sufficient as possible for reuse in other
forms/reports/modules when the query is called in those places.

This is the criteria I already set for this:
like iif(isnull([Par1]),"*",[Par1])

In this case, if I do not enter anything into the parameter, or I put in
the asterisk I get all the records, while if I put in the full text of the
parameter I get only the record matching that parameter. However, this
still requires me to set the parameter, either in code or manually when I
run the query from the query gui.

A workaround, which in this case is simple, but goes against the grain
anyway, looks like this:

Public Function setQueryDefaults(qryName As String) As DAO.QueryDef
Dim qry As DAO.QueryDef
Set qry = CurrentDb.QueryDefs(qryName)
Dim i As Integer
For i = 0 To qry.Parameters.Count - 1
qry.Parameters(i) = Null
Next i
Set setQueryDefaults = qry
End Function

Of course it can be expanded to handle querydef object initialization
parameters (e.g., dbOpenDynaset), but this would just add to the overhead
to the program. Also, it assumes that the parameters on the queries are
properly set to accept null values.

Of course migrating from such this setup to one in which default
parameters are supported in the desired fashion would be somewhat of a
nightmare, assuming I rewrite the queries for the next database engine.

Marc

Nov 16 '05 #2

"PC Datasheet" <no****@nospam.spam> schreef in bericht news:t0****************@newsread1.news.atl.earthli nk.net...

<snipped the message and all the advertising stuff>

To the OP: Beware of this guy!!

Steve just does *not* care about the newsgroups. He has *no ethics at all*.
Steve *only* cares about making *money*, and he acts as if the groups are his private hunting ground.

-- He abuses this group and others for job-hunting and advertising over and over again
-- He is insulting lots of people here when they ask him to stop this
-- He posted as Steve, Ron, Tom, Rachel, Kathy, Kristine, Heather and ??? while asking questions
(the latest 'star's': 'Access Resource' and Tom no***@email.com and Andy)
-- He tries to sell a CD ($125,--) with FREE code he gathered from these groups here
-- There even has been a 'Scam-alert' about him which has been explained recently in the thread 'To all':
http://groups.google.com/group/comp....954261f9?hl=en
-- Also recently it became clear that he has been spamming innocent people asking questions:
http://groups.google.com/group/comp....3e5f58ad?hl=en

So why would ANYBODY ever trust a person like him and hire him?
************************************************** ******

Explanation and more links on this answer:
http://home.tiscali.nl/arracom/stopsteve.html

Arno R
Nov 17 '05 #3
What is the point of opening a query object using an already existing
query and then changing the sql property? If I understand Access
correctly, when you have a compiled query Access will optimize the way
it runs the query as it is run more. If you change the sql property,
which basically means you are erasing the existing sql statement and
putting your own in, the optimization information will be erased as it
is no longer valid, hence defeating the purpose of using a compiled
query. If for some reason I need to put in my own sql statement I don't
need to attach a query to the querydef object, but can put in my own
text in th sql property.

If the above is true, then this really doesn't help.

Marc
PC Datasheet wrote:
You can do something like this ---
dim qry as dao.querydef
dim param as parameter
dim SQLStrBlah As String
dim SQLStrBlahBlah As String
set SQLStrBlah = "Select ..... Where..."
set SQLStrBlahBlah = "Select ..... Where..."
set qry = currentdb.querydefs("myquery")
If <Whatever > Then
qry.sql = SQLStrBlah
Else
qry.sql = SQLStrBlah
End If
For each param in qry.parameters
param.value = EVAL(param.name)
Net param

Nov 17 '05 #4
The above may be true sometimes, and not so true others.

In JET 3.5 this was true:

(from Jet Database Engine ProgrammersGuide)

"Parameter queries can only be implemented by using a stored query.
Because stored queries have a precompiled query plan, parameter queries
that contain parameters on indexed fields may not execute efficiently.
Because the query engine does not know the values to be passed in a
parameter in advance, it can only guess as to the most efficient query
plan. In some instances, you can substantially improve performance by
replacing a stored parameter query with a temporary query. To do this,
create the SQL string in code and pass it to the Database object's
OpenRecordset method or Execute method"

Is it still true in JET 4.0? I don't know.

I do know that the notion that "compiled" queries are noticeably faster
than "temporary" queries is not something that I can attest to.

Nov 17 '05 #5
lylefair <ly***********@aim.com> wrote:
The above may be true sometimes, and not so true others.

In JET 3.5 this was true:

(from Jet Database Engine ProgrammersGuide)

"Parameter queries can only be implemented by using a stored query.
Because stored queries have a precompiled query plan, parameter
queries that contain parameters on indexed fields may not execute
efficiently. Because the query engine does not know the values to be
passed in a parameter in advance, it can only guess as to the most
efficient query plan. In some instances, you can substantially
improve performance by replacing a stored parameter query with a
temporary query. To do this, create the SQL string in code and pass
it to the Database object's OpenRecordset method or Execute method"

Is it still true in JET 4.0? I don't know.

I do know that the notion that "compiled" queries are noticeably
faster than "temporary" queries is not something that I can attest to.


With CurrentDb().CreateQueryDef("", _
"PARAMETERS x DateTime; UPDATE ... x ...;")
For Each i In x 'process all values in some paramarray x()
.Parameters!x.Value = i
.Execute
Next
End With

Parameters, temporary. Better? Déjà vu?

--
Paul
Nov 18 '05 #6
I've not seen this way of doing temp queries/parameters before; looks
great (assuming no gotchas).

Nov 18 '05 #7
lylefair <ly***********@aim.com> wrote:
I've not seen this way of doing temp queries/parameters before; looks
great (assuming no gotchas).


Hi, google for parametricset.
--
Paul


Nov 18 '05 #8
kaniest wrote:
lylefair <ly***********@aim.com> wrote:
<snip>
With CurrentDb().CreateQueryDef("", _
"PARAMETERS x DateTime; UPDATE ... x ...;")
For Each i In x 'process all values in some paramarray x()
.Parameters!x.Value = i
.Execute
Next
End With

Parameters, temporary. Better? Déjà vu?

Perhaps I was a bit to vague regarding my question. When I say
"optional parameter", I mean that the parameter need not be set at all
in code. My workaround merely iterates through all the parameters and
sets them to null. If a query has alot of parameters, there could be a
performance hit. The whole point of an optional parameter is that I can
either use it or not. This is just like optional parameters in vba
functions. In vba functions the code still has to handle the existence
or not of the optional parameter. This should be the same with a
parameter query.

Also, maybe someone could clarify the whole point of parameters in
temporary queries. Since these don't get compiled at any point you
would probably have better performance creating a query string
containing all the criteria built in and then executing the query using
the whole string. I can see that if a temporary query is being reused
it might be a bit convenient to create a temporary parameter query and
pass the parameters each time the query is executed. Maybe someone can
comment on how this would perform vs. reconstructing the query string
each time.

As I mentioned in my first post one of the reasons for my madness is to
allow for easy conversion to a backend with minimal code changes. Using
queries that approximate those on other database engines would allow for
upgrading without having to code the application from scratch.

Marc
Nov 18 '05 #9
Marc DVer <ma**@not.in.use> wrote:
kaniest wrote:
Parameters, temporary. Better? Déjà vu?

This hinted at some point in this thread being discussed before.
As I mentioned in my first post one of the reasons for my madness is
to allow for easy conversion to a backend with minimal code changes. Using
queries that approximate those on other database engines would
allow for upgrading without having to code the application from
scratch.


So, since there's no common sql construct for optional parameters,
it must be handled in the calling code anyway. Or?

--
Paul
Nov 18 '05 #10
Marc DVer wrote:
What is the point of opening a query object using an already existing
query and then changing the sql property?


It's no use asking "Steve" to explain anything, he isn't capable. Once
he's got his advert into a thread it's the last you see of him.
Nov 19 '05 #11
Why are you using a fake name, Keith? Arno R would stick up for you.

There was no need to respond. Lyle and Paul answered Marc's question. All
you did was pollute the newsgroup with worthless garbage!!!

--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
re******@pcdatasheet.com
www.pcdatasheet.com

If you can't get the help you need in the newsgroup, I can help you for a
very reasonable fee. Over 1000 Access users have come to me for help.
Need a month calendar or 7 day calendar? Need appointment scheduling? Need
room reservations scheduling? Need employee work scheduling? Contact me!


"Some Bloke" <he**@there.com> wrote in message
news:dl**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
Marc DVer wrote:
What is the point of opening a query object using an already existing
query and then changing the sql property?


It's no use asking "Steve" to explain anything, he isn't capable. Once
he's got his advert into a thread it's the last you see of him.

Nov 19 '05 #12

"PC Datasheet" <no****@nospam.spam> schreef in bericht news:tF****************@newsread3.news.atl.earthli nk.net...

If you can't get the help you need in the newsgroup, I can help you for a
very reasonable fee. Over 1000 Access users have come to me for help.
Need a month calendar or 7 day calendar? Need appointment scheduling? Need
room reservations scheduling? Need employee work scheduling? Contact me!


In some threads Steve needs/wants/choses to advertise more shit... ;-)

Arno R
Nov 19 '05 #13
"PC Datasheet" <no****@nospam.spam> wrote in message
news:tF****************@newsread3.news.atl.earthli nk.net...
Why are you using a fake name, Keith? Arno R would stick up for you.

Because that was posted from home and that's the handle I now use from
there. Trust me, I do not need to hide from the likes of you. For the
pollution and wothless garbage, read on.
If you can't get the help you need in the newsgroup, I can help you for a
very reasonable fee. Over 1000 Access users have come to me for help.
Need a month calendar or 7 day calendar? Need appointment scheduling? Need
room reservations scheduling? Need employee work scheduling? Contact me!

Nov 21 '05 #14
So all the times in the past and including this post where we see Keith W,
you are posting from work on your employer's time?????????

--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
re******@pcdatasheet.com
www.pcdatasheet.com

If you can't get the help you need in the newsgroup, I can help you for a
very reasonable fee. Over 1000 Access users have come to me for help.
Need a month calendar or 7 day calendar? Need appointment scheduling? Need
room reservations scheduling? Need employee work scheduling? Contact me!


"Keith W" <he**@there.com> wrote in message
news:43**********@glkas0286.greenlnk.net...
"PC Datasheet" <no****@nospam.spam> wrote in message
news:tF****************@newsread3.news.atl.earthli nk.net...
Why are you using a fake name, Keith? Arno R would stick up for you.


Because that was posted from home and that's the handle I now use from
there. Trust me, I do not need to hide from the likes of you. For the
pollution and wothless garbage, read on.
If you can't get the help you need in the newsgroup, I can help you for a
very reasonable fee. Over 1000 Access users have come to me for help.
Need a month calendar or 7 day calendar? Need appointment scheduling?
Need
room reservations scheduling? Need employee work scheduling? Contact me!


Nov 21 '05 #15
"PC Datasheet" <no****@nospam.spam> wrote in message
news:ai****************@newsread2.news.atl.earthli nk.net...
So all the times in the past and including this post where we see Keith W,
you are posting from work on your employer's time?????????

What makes you think that ??!!!????!!!!!??111!555745!!!!FFF??

?
Nov 21 '05 #16

"PC Datasheet" <no****@nospam.spam> schreef in bericht news:ai****************@newsread2.news.atl.earthli nk.net...

<snipped rubbish>
If you can't get the help you need in the newsgroup, I can help you for a
very reasonable fee. Over 1000 Access users have come to me for help.
We don't see ANY of these users here to defend you or support you...
Need a month calendar or 7 day calendar? Need appointment scheduling? Need
room reservations scheduling? Need employee work scheduling? Contact me!


Need to advertise like a maniac? Contact Steve!

Arno R
Nov 21 '05 #17
If you post by a pseudoname from home, WHERE DO YOU post by Keith W from????

--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
re******@pcdatasheet.com
www.pcdatasheet.com

If you can't get the help you need in the newsgroup, I can help you for a
very reasonable fee. Over 1000 Access users have come to me for help.
Need a month calendar or 7 day calendar? Need appointment scheduling? Need
room reservations scheduling? Need employee work scheduling? Contact me!


"Keith W" <he**@there.com> wrote in message
news:43**********@glkas0286.greenlnk.net...
"PC Datasheet" <no****@nospam.spam> wrote in message
news:ai****************@newsread2.news.atl.earthli nk.net...
So all the times in the past and including this post where we see Keith
W, you are posting from work on your employer's time?????????

What makes you think that ??!!!????!!!!!??111!555745!!!!FFF??

?

Nov 21 '05 #18

"PC Datasheet" <no****@nospam.spam> schreef in bericht news:JT****************@newsread2.news.atl.earthli nk.net...
If you post by a pseudoname from home, WHERE DO YOU post by Keith W from????


Is this any of *your* business Steve??, or Ron, Tom, Rachel, Kathy, Kristine, Heather and ???

<snipped all the bloody advertising>

Arno R
Nov 21 '05 #19
PC Datasheet wrote:
So all the times in the past and including this post where we see Keith W,
you are posting from work on your employer's time?????????


If you consider that unprofessional behavior please advise the rest of
us in this NG of an effective strategy for dealing with such scofflaws
in the future.

James A. Fortune

Nov 23 '05 #20
<ji********@compumarc.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
If you consider that unprofessional behavior please advise the rest of
us in this NG of an effective strategy for dealing with such scofflaws
in the future.

James A. Fortune

First, you should not use such an obvious fake name like "James Fortune".
James is obviously a fake name.Try something like "Mac Spreadsheet". The
"Mac" adds a bit of ethnic authenticity to the name.

Second, you should not engage in a battle of wits with an unarmed opponent.
;-)

John... Visio MVP
Nov 23 '05 #21
"PC Datasheet" <no****@nospam.spam> wrote in message
news:JT****************@newsread2.news.atl.earthli nk.net...
If you post by a pseudoname from home, WHERE DO YOU post by Keith W
from????

It is indeed none of your business and it beggars belief why anyone would be
interested.
Nov 23 '05 #22

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

Similar topics

2
1642
by: et | last post by:
I have the following function that uses an optional parameter, and it sets a default, as the program says I have to. Yet it doesn't pick up the default value. Why is this, what am I doing wrong....
10
6831
by: John Austin | last post by:
I am migrating my first vb6 app to vb.net 2003. In the vb6 app a number of subs had optional date parameters: Sub Fred ( ... ,optional FromDate as Date = 0...) I need something like zero in...
12
2276
by: Nick Hounsome | last post by:
Can anyone tell me what the rational is for not supporting optional arguments. It is obviously a trivial thing to implement and, since C++ has them, I would not expect them to be omitted without...
4
5919
by: Joe HM | last post by:
Hello - I realize that there is no more IsMissing function in VB.NET but how can I have a boolean argument that is optional and in the code I need to determine whether it was passed it or not? ...
14
3245
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
2
11893
by: Oenone | last post by:
In our applications, we use the special value of DateTime.MinValue to represent "null dates" throughout all our code. We recently ran into an issue where we wanted an optional date parameter for a...
12
2649
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be...
2
1301
by: Curious | last post by:
I have two methods that are almost identical, except for the "HtmlLogger varLogger" parameter passed to #2. In #1, it uses a default HtmlLogger "logger" while in #2, it uses "varLogger" parameter....
7
7039
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
7228
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
7128
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
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7393
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
5635
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.