Link to home
Start Free TrialLog in
Avatar of ricswika
ricswika

asked on

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
Avatar of Ivanov_G
Ivanov_G
Flag of Bulgaria image

 with Query1 do
    begin
      SQL.Text := 'SELECT * FROM YOUR_TABLE WHERE ID = :ID';
      Params[0].Value := 1;
    end;
Avatar of aikimark
Use the Params array to assign the values.

Example:
MyQuery.Params[1].AsDate:= StrToDate( '1.1.1998' );
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. :-)
Avatar of ricswika
ricswika

ASKER

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


.
ASKER CERTIFIED SOLUTION
Avatar of ricswika
ricswika

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial