?
Solved

MS Access and Delphi

Posted on 2003-03-13
9
Medium Priority
?
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

770 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