Link to home
Start Free TrialLog in
Avatar of error77
error77

asked on

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

Avatar of jimyX
jimyX

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.
ASKER CERTIFIED SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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

Avatar of error77

ASKER

Thanks :)