473,398 Members | 2,188 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,398 software developers and data experts.

How to use parameterized query with DAO Recordset?

I'm trying to open a Recordset based on a parameterized query. I'm kind of
new to parameterized queries, so I'm sure I'm missing something simple.

Set qdfs = db.QueryDefs
Set qdf = qdfs("qryInvoices")
qdf.Parameters("prmInv") = strInvoice
qdf.Parameters("prmCid") = lngCustomerID
Set rst = db.OpenRecordset("qryInvoices")

[qryInvoices]
SELECT Customer_ID
FROM tblCustomers
WHERE (HyperlinkPart([Invoice], 0) = prmInv) And ([Customer_ID] =
prmCid);

Why doesn't this work? How do I open a Recordset based on a parameterized
query?

Thank in advance.
Nov 13 '05 #1
8 12894
Perhaps
Set rst=qdf.OpenRecordset([Type], [Options], [LockEdit])
?

Nov 13 '05 #2
You set the parameters for qdf, but then you opened a second copy of the
query when you went back to using db in the statement db.OpenRecordset.
Instead, use qdf.OpenRecordset. You won't need to specify the query name,
because qdf is already a query.

Set rst = qdf.OpenRecordset

--
Wayne Morgan
MS Access MVP
"deko" <de**@nospam.com> wrote in message
news:8Z********************@comcast.com...
I'm trying to open a Recordset based on a parameterized query. I'm kind
of new to parameterized queries, so I'm sure I'm missing something simple.

Set qdfs = db.QueryDefs
Set qdf = qdfs("qryInvoices")
qdf.Parameters("prmInv") = strInvoice
qdf.Parameters("prmCid") = lngCustomerID
Set rst = db.OpenRecordset("qryInvoices")

[qryInvoices]
SELECT Customer_ID
FROM tblCustomers
WHERE (HyperlinkPart([Invoice], 0) = prmInv) And ([Customer_ID] =
prmCid);

Why doesn't this work? How do I open a Recordset based on a parameterized
query?

Thank in advance.

Nov 13 '05 #3
> Set rst = qdf.OpenRecordset

Thanks - that was easy. I also discovered I had the constant wrong in the
WHERE statement - shd be 2 not 0

Set qdfs = db.QueryDefs
Set qdf = qdfs("qryInvoices")
qdf.Parameters("prmInv") = strInvoice
qdf.Parameters("prmCid") = lngCustomerID
Set rst = qdf.OpenRecordset

[qryInvoices]
SELECT Customer_ID
FROM tblCustomers
WHERE (HyperlinkPart([Invoice], 2) = prmInv) And ([Customer_ID] = prmCid);
Nov 13 '05 #4
"deko" <de**@nospam.com> wrote in
news:8Z********************@comcast.com:
I'm trying to open a Recordset based on a parameterized query.
I'm kind of new to parameterized queries, so I'm sure I'm missing
something simple.

Set qdfs = db.QueryDefs
Set qdf = qdfs("qryInvoices")
qdf.Parameters("prmInv") = strInvoice
qdf.Parameters("prmCid") = lngCustomerID
Set rst = db.OpenRecordset("qryInvoices")

[qryInvoices]
SELECT Customer_ID
FROM tblCustomers
WHERE (HyperlinkPart([Invoice], 0) = prmInv) And
([Customer_ID] =
prmCid);

Why doesn't this work? How do I open a Recordset based on a
parameterized query?


Lyle has asked your question, but I don't quite understand why you
have two paremeters. Can't an invoice be billed to only one
customer?

I just don't use parameterized queries, except for quick-and-dirty
forms and reports. I *never* use them in DAO, because it's messy to
assign the parameters, when it's easier to just write a WHERE clause
directly, with dynamic SQL, and open a recordset on that.

If you're working with a server back end, perhaps the parameterized
version will be more efficient, but you didn't say that was the
case.

I simply question the utility of using a saved QueryDef in this
context in the first place.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #5
> I just don't use parameterized queries, except for quick-and-dirty
forms and reports. I *never* use them in DAO, because it's messy to
assign the parameters, when it's easier to just write a WHERE clause
directly, with dynamic SQL, and open a recordset on that.


I think what you mean is this:

qdf.Sql = strSql

which is what I do most of the time. But if the parameter is the only thing
that changes or if the string contains quotes or special characters I like
using parameterized queries. As you know, one man's mess is another man's
elegance.
Nov 13 '05 #6
"deko" <de**@nospam.com> wrote in
news:TL********************@comcast.com:
I just don't use parameterized queries, except for
quick-and-dirty forms and reports. I *never* use them in DAO,
because it's messy to assign the parameters, when it's easier to
just write a WHERE clause directly, with dynamic SQL, and open a
recordset on that.
I think what you mean is this:

qdf.Sql = strSql


No, I don't mean anything of the sort.

I don't edit stored queries in code, except in very special cases
(such as wanting to base a Microsoft graph on a TOP N query).
which is what I do most of the time. But if the parameter is the
only thing that changes or if the string contains quotes or
special characters I like using parameterized queries. As you
know, one man's mess is another man's elegance.


I don't see the point of using them. I prefer to write the SQL
deynamically and execute it or open a recordset on it, rather than
having to deal with setting parameters, some of which may not always
be necessary in a particular context.

I do use a few parameter queries, but those are queries that are
never used in code.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #7
> I don't see the point of using them. I prefer to write the SQL
deynamically and execute it or open a recordset on it, rather than
having to deal with setting parameters, some of which may not always
be necessary in a particular context.


So do you mean:

db.Execute strSql

?

But isn't a stored query faster than this? From what I've read, the
advantage of a stored query is that it's already compiled and JET can
optimize it.
Nov 13 '05 #8
"deko" <de**@nospam.com> wrote in
news:HY********************@comcast.com:
I don't see the point of using them. I prefer to write the SQL
deynamically and execute it or open a recordset on it, rather
than having to deal with setting parameters, some of which may
not always be necessary in a particular context.
So do you mean:

db.Execute strSql

?

But isn't a stored query faster than this? . . .


Only under the most unusual circumstances, especially in the case of
action queries (the only kind that you can execute).
. . . From what I've read,
the advantage of a stored query is that it's already compiled and
JET can optimize it.


That only matters for complex queries that benefit from
optimization. I've never seen any issues with dynamic SQL being
slow.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #9

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

Similar topics

1
by: gary b | last post by:
Hello When I use a PreparedStatement (in jdbc) with the following query: SELECT store_groups_id FROM store_groups WHERE store_groups_id IS NOT NULL AND type = ? ORDER BY group_name
4
by: Gilberto Campos | last post by:
Hi all. I am having a strange problem. I am developping an application that acceses an Access db through Jet (.UDL files). I have writen parametric INSERT queries that work fine. I am now...
0
by: totierne | last post by:
comp.databases.ms-access, I want to know how to use Oracle views with session variables in Access. The parameterised views in access, are migrated to views with per session variables. The...
2
by: deko | last post by:
Is it possible to build a parameterized query from another parameterized query? I've tried two variations of this and can't seem to get it to work (using DAO). Any suggestions welcome! I...
11
by: anony | last post by:
Hello, I can't figure out why my parameterized query from an ASP.NET page is dropping "special" characters such as accented quotes & apostrophes, the registered trademark symbol, etc. These...
2
by: JSheble | last post by:
After building a parameterized ADO query, is there a method or a statement where you could see the actual query, with the parameterized values included?? -- Using Opera's revolutionary e-mail...
6
by: kenshiro | last post by:
Hi All, I'm having a problem with some VBA code in one of my Access 2003 databases. I'm getting the following error when running code behind a command button on a form: "Item not found in this...
8
by: Roland Hall | last post by:
In Access you use "*" + + "*", + can be replaced with & Calling a parameterized query in Access requires % be used in place of *, however, all that I have read show dynamic SQL passed to Access: ...
2
by: mcalex | last post by:
Hi, I'm having trouble trying to populate a list box with data from a parameterized query. If I set the rowsource property to the query name, when the form opens I get the parameter values dialog,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.