Solved

Passing parameters to MS Access query?

Posted on 2004-09-01
7
648 Views
Last Modified: 2010-04-05
How can I pass parameters to an MS-Access query?

I have queries located in an Access database that require parameters as input. When you run one of these queries in Access, it prompts for each parameter before running. How can I pass these parameters to the Access query directly from Delphi 3 and retrieve the resulting dataset in Dephi.

I know I can copy the SQL from Access into a Tquery component at design time and supply the parameters at run time that way. I tried that and it does work, but I’d like to keep my design layered such that the query logic (SQL) remains inside the Access database. This way when the database structure changes, I don’t necessarily need to change my code. Additionally, many of these queries are quite complex and it is a serious waste of time to touch them twice.

Any input would be appreciated.

Rick
0
Comment
Question by:ricswika
7 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 11958482
 with Query1 do
    begin
      SQL.Text := 'SELECT * FROM YOUR_TABLE WHERE ID = :ID';
      Params[0].Value := 1;
    end;
0
 
LVL 45

Expert Comment

by:aikimark
ID: 11959798
Use the Params array to assign the values.

Example:
MyQuery.Params[1].AsDate:= StrToDate( '1.1.1998' );
0
 
LVL 30

Expert Comment

by:third
ID: 11961208
MS Access queries with parameters in the MDB file will be interpreted by Delphi as stored procedures. So you will have to use a StoredProc component, add the parameter, and pass the parameter values there. :-)
0
 
LVL 1

Author Comment

by:ricswika
ID: 11964482
The point here was to keep all the SQL in the access database, so the first two guys are off base. Your approach will work if I copy the SQL from the access database into the TQuery component, but I don't want to do that. The database is normalized, so it contains a lot of little tables linked through relationships. I wan't to keep all the details of the database internals out of delphi, and only talk to the database through a few standardized queries.

'Third' seems to have the right idea. I've been expirementing with the StroredProc component, but haven't gotten it to work yet. Remember, I am using Delphi 3 for this. This is an old project :)

First I set the Database property of TStoredProc to reference by Access Database. I have no problems seeing all of the tables and queries using TTable or TQuery, so I know this part is correct.

Next, I enter MyQueryName into the TStoredProc.StoredProcName. This brings the parameters and fields into the TStoredProc.Params object. So far so good.

Now If I try opening the TStoredProc, it complains that a parameter type is unknown. So I go into the Params property and define the types for all of the parameters listed there. Now when I try opening the TStoredProc it complains "Object Not Found". Say What?

This is as far as I got. Any suggestions?

Thanks
Rick


.
0
 
LVL 1

Accepted Solution

by:
ricswika earned 0 total points
ID: 11967635
I found the answer myself. Thanks anyway.

I needed to set TQuery.SQL to something like this:

exec qMyAccessQuery 'Param1', 'Param2', ...'ParamN'

This works find through BDE in Delphi3. No need for ADO components.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now