Mega Code Archive

 
Categories / Delphi / ADO Database
 

Query an Access 2000 database with parameters using ADO

Title: Query an Access 2000 database with parameters using ADO Question: How do I query an Access 2000 database Answer: It's a must for working with ADO and Access 97/2000. At work we use a database for managing user and their software. My exemple will explain how to show user that work with specific OS. Here how it work First put a TADOQuery on your form. Open the object Inspector On "Connection string" click the three dot (...) now click the build button. Select Microsoft Jet Ole DB Engine and click next select the path for the data base. If you don't use password protected database remove "Admin". (Test your connection to see if you do it right.). Then OK. Put a simple EditBox with the name "WhatWeAreLooking" A simple button, DataSource connected to ADOQuery1, finally a DBGrid connected to Datasource1. Double click on the button to access the unit of the form. ************* ADOQuery1.Close; //A good thing to begin with closing the query ADOQuery1.SQL.clear; //Clear the SQL //Now we add the SQL statement (getting all the the info from //the table InfoComputer that meet a specific os (param1) ADOQuery1.sql.Add('select * from InfoComputer where os = :param1'); {Call ParseSQL to parse the SQL statement for parameters. From Delphi HELP : function ParseSQL(SQL: String; DoCreate: Boolean): String; -SQL is a String containing the SQL statement to parse. -DoCreate indicates whether to clear all existing parameter definitions before parsing the SQL statement. } ADOQuery1.Parameters.ParseSQL(ADOQuery1.SQL.Text, True); // Give a value to our parameter ADOQuery1.Parameters.ParamByName('param1').value := WhatWeAreLooking.text; //Open the query ADOQuery1.open; **************** Why we parse the SQL ? Because if we don't we will get the following error message "Parameter "ParamName" Not Found error when using an ADOQuery and Parameters " Source : http://community.borland.com/article/0,1410,20420,00.html Start the programe and put your Parameters in the text box and click the button. you will have the result in your DBGrid