Solved

Passing parameters to MS Access query?

Posted on 2004-09-01
7
652 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
creating manifest for my dll that called from activex 6 100
Delphi XE10, MySQL Query 4 127
When I am typing into a TEdit last word repeats 7 119
delphi parse string to params 3 100
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

21 Experts available now in Live!

Get 1:1 Help Now