Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Passing parameters to MS Access query?

Posted on 2004-09-01
7
654 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 Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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