Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 662
  • Last Modified:

Passing parameters to MS Access query?

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
ricswika
Asked:
ricswika
1 Solution
 
Ivanov_GCommented:
 with Query1 do
    begin
      SQL.Text := 'SELECT * FROM YOUR_TABLE WHERE ID = :ID';
      Params[0].Value := 1;
    end;
0
 
aikimarkCommented:
Use the Params array to assign the values.

Example:
MyQuery.Params[1].AsDate:= StrToDate( '1.1.1998' );
0
 
thirdCommented:
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
 
ricswikaAuthor Commented:
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
 
ricswikaAuthor Commented:
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now