Assistance needed with ADO DB Connection

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

error77Asked:
Who is Participating?
 
jimyXConnect With a Mentor Commented:
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
 
jimyXCommented:
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
 
jimyXCommented:
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
 
Ephraim WangoyaCommented:
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
 
error77Author Commented:
Thanks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.