Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

MS Access and Delphi

Posted on 2003-03-13
9
Medium Priority
?
481 Views
Last Modified: 2010-04-04
I have an MS Access DB that I have a front end built for. I am now trying to connect the forms to the DB.

How do I call a query from within delphi and get a result set?
Can someone point me to a tutorial about how to do this? Or a specific example of what stuff I need to put on the form?


I am using Delphi 7 and Access 97.

WormsWorms
0
Comment
Question by:WormsWorms
7 Comments
 

Expert Comment

by:yurastaub
ID: 8128903
Hi,
Lets imagine that you need to browse Access data in grid.
So, on form put TDBGrid component, TDataSource, TTable, TDatabase.
Lets set properties:
Database1.DatabaseName:='tryDB';
Database1.DriverName:=MSACCESS;
Database1.LoginPromt:=false;
Database1.Params.Clear;
Database1.Params.Add('DATABASE NAME=E:\Projects\GardenGuard\Project\pattern.mdb
');
Database1.Connected:=true;
TDBGrid.DataSource:=DataSource1;
DataSource1.DataSet:=Table1;
Table1.DatabaseName:='tryDB';

Now you can try select for Table1 TableName from List Delphi should offer. After that can set property:
Table1.Active:=true;

Now you must see in Grid1 table data.

Yura
0
 
LVL 3

Expert Comment

by:emadat
ID: 8129677
0
 
LVL 17

Accepted Solution

by:
geobul earned 1000 total points
ID: 8129820
Hi,

Use ADO components. Place one TADOConnection on a form (or better use DataModule for all db components). Place one TADOQuery also. Set ADOQuery1.Connection property to the ADOConnection1.

1. Connecting to the db (in an event which gets fired on program startup, MainForm.OnActivate for example):

with ADOConnection1 do begin
  ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Password=MyPass;Data Source=C:\test.mdb;Persist Security Info=True';
  LoginPrompt := false; // dont prompt for login/pass
  Connected := true;
end;

You may not pass username and password directly in the connection string. Then LoginPrompt should be true and there will be a login dialog box automatically.

2. Retreiving information

with ADOQuery1 do begin
  Close;
  SQL.Clear;
  SQL.Add('SELECT * FROM Table1');
  SQL.Add('WHERE Field1=' + QuotedStr(Edit1.Text));
  Open;
  First;
  while not EOF do begin
    // do something with the current record:
    ShowMessage(FieldByName('Field2').AsString);
    Next;
  end;
  Close;
end;

Regards, Geo
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:hetturki
ID: 8142012
Hi
from ADO components Place one TADOConnection ,one TADOQuery and one DataSource on a form (its better to put the db component in dateModel)

1- connecting database ADOConnection1.connectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Password=MyPass;Data Source=C:\employee.mdb;Persist Security Info=True';
 ADOConnection1.LoginPrompt := false;
 ADOConnection1.Connected := true;

set ADOQuery1.connection property to ADOConnection1
connect the DataSource with ADOQuery1

2_ Put dbgrid on the form
connect DBgrid with the ADOQuery1 (dataset property of dbgrid)
3_ place bitbtn on the form
4_ Double click on DBgrid and add all FIELDS
5_ in bitbtn1.click procedure place the following code


with ADOQuery1 do begin
 
 Close;
 SQL.Clear;
 SQL.Add('SELECT * FROM EMP');
 SQL.Add('WHERE sal >= 250');
 Open;

Run the project and click on the bitbtn you will see the result

BRGS


0
 

Author Comment

by:WormsWorms
ID: 8160003
Thanks guys. I actually figured it out from the help file.



WormsWorms
0
 

Author Comment

by:WormsWorms
ID: 8160130
And besides, who should I give the points to. 3 people have answers. I actually figured it out from the help file.

BUT, geobul is whining, so I am gonna give him the pionts, and I guess you other guys get screwed.

Sorry

WormsWorms
0
 

Expert Comment

by:CleanupPing
ID: 9316797
WormsWorms:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

577 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