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:
Ā
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;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
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;
ASKER
Thanks :)
Open in new window
Or you can set the properties of the DataSource, DBNavigator, and the DBGrid at the design time.