[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Assistance needed with ADO DB Connection

Posted on 2011-05-09
5
Medium Priority
?
358 Views
Last Modified: 2012-05-11
Hi all,

I am using a function to create a database connection to an access database and it works great.

I have a DBNavigator and Grid Component too.

Could someone help me with some code to get them connected too please?

Here is the code im using:

 
function TForm1.InitConnection(const ADBName: string): Boolean;
var
  ConnString, DatabaseName: string;
begin
  DatabaseName := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + ADBName;
  ConnString := Format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False', [DatabaseName]);
  ADOConnection1.ConnectionString := ConnString;
  ADOConnection1.LoginPrompt := False;
  ADOConnection1.Connected := True;
  Result := ADOConnection1.Connected;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 InitConnection('Database1.mdb');
 
end;

Open in new window

0
Comment
Question by:error77
  • 3
5 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 35725473
Drop DataSource (from DataAccess Tab) and of course the DBNavigator and the DBGrid, then use this code:
function TForm1.InitConnection(const ADBName: string): Boolean;
var
  ConnString, DatabaseName: string;
begin
  DatabaseName := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + ADBName;
  ConnString := Format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False', [DatabaseName]);
  ADOConnection1.ConnectionString := ConnString;
  ADOConnection1.LoginPrompt := False;
  ADOConnection1.Connected := True;
  Result := ADOConnection1.Connected;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   InitConnection('Database1.mdb');
   ADOTable1.Connection := ADOConnection1;
   ADOTable1.TableName:= 'TableName';  // The table that you want to view in the DBGrid
   DataSource1.DataSet := ADOTable1;
   DBNavigator1.DataSource := DataSource1;
   DBGrid1.DataSource := DataSource1;
   ADOTable1.Open;
end;

Open in new window

Or you can set the properties of the DataSource, DBNavigator, and the DBGrid at the design time.
0
 
LVL 24

Accepted Solution

by:
jimyX earned 2000 total points
ID: 35725485
That can be also in the InitConnection function:
function TForm1.InitConnection(const ADBName: string): Boolean;
var
  ConnString, DatabaseName: string;
begin
  DatabaseName := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + ADBName;
  ConnString := Format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False', [DatabaseName]);
  ADOConnection1.ConnectionString := ConnString;
  ADOConnection1.LoginPrompt := False;
  ADOConnection1.Connected := True;
  ADOTable1.Connection := ADOConnection1;
  ADOTable1.TableName:= 'TableName';
  DataSource1.DataSet := ADOTable1;
  DBNavigator1.DataSource := DataSource1;
  DBGrid1.DataSource := DataSource1;
  Result := ADOConnection1.Connected;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   InitConnection('Database1.mdb');
   ADOTable1.Open;
end;

Open in new window

0
 
LVL 24

Expert Comment

by:jimyX
ID: 35725502
It's better if the TableName is passed as a parameter:
function TForm1.InitConnection(const ADBName, TblName: string): Boolean;
var
  ConnString, DatabaseName: string;
begin
  DatabaseName := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + ADBName;
  ConnString := Format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False', [DatabaseName]);
  ADOConnection1.ConnectionString := ConnString;
  ADOConnection1.LoginPrompt := False;
  ADOConnection1.Connected := True;
  ADOTable1.Connection := ADOConnection1;
  ADOTable1.TableName:= TblName;
  DataSource1.DataSet := ADOTable1;
  DBNavigator1.DataSource := DataSource1;
  DBGrid1.DataSource := DataSource1;
  Result := ADOConnection1.Connected;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   InitConnection('Database1.mdb', 'TableName');
   ADOTable1.Open;
end;

Open in new window

0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35726091
try this
procedure TForm3.InitGridConnection(const ATableName: string);
//for table component
begin
  if ADOTable1.Active then
    ADOTable1.Active := False;

  ADOTable1.Connection := ADOConnection1;
  ADOTable1.TableName := ATableName;
  ADOTable1.Open;

  Datasource1.DataSet := ADOTable1;
  DBGrid1.DataSource := Datasource1;
  DBNavigator1.DataSource := Datasource1;
end;

procedure TForm3.InitGridConnection1(const ATableName: string);
//for a query component
begin
  if ADOQuery1.Active then
    ADOQuery1.Active := False;

  ADOQuery1.Connection := ADOConnection1;
  ADOQuery1.SQL.Text := 'Select * from ' + ATableName;
  ADOTable1.Open;

  Datasource2.DataSet := ADOQuery1;
  DBGrid1.DataSource := Datasource2;
  DBNavigator1.DataSource := Datasource2;
end;

Open in new window

0
 

Author Closing Comment

by:error77
ID: 35726447
Thanks :)
0

Featured Post

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!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

872 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