Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ADO Connection at run time

Status
Not open for further replies.

MLNorton

Programmer
Nov 15, 2009
134
US
I am trying to create an ADO Connection with a Table at Run Time and activate a DBGrid with the data. I have researched the web and created the following code that I hope will create the ADOConnection and the Table.

procedure TForm1.Button_CreateClick(Sender: TObject);
var
DskDrive: string;
MyPath: string;
MYDb: TADOConnection;
MyTbl1: TADOTable;
MyTbl2: TADOTable;
MyDBS: TDataSource;

begin
DskDrive := ExtractFileDrive(ParamStr(0));

MyPath := DskDrive + '//My Files/Columbia/Data/Columbia-2012.mdb';

MyDb := TADOConnection.Create(Self);
MyDb.Close;
MyDb.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=' + MyPath + ';' +
'Persist Security Info=True;';
MyDb.LoginPrompt :=False;
MyDb.Connected := True;
MyDb.Open;

MyTbl1 := TADOTable.Create(Self);
MyTbl1.Active := false;
MyTbl1.Connection := MyDb;
MyTbl1.TableName := 'Table1';
MyTbl1.Open;

MyDBS := TDataSource.Create(Self);
MyDBS.DataSet := MyTbl1;
end;

Please refiew this code and advise me if it is correct or wher it needs to be changed.

Once this works I need to configure the DBGrid to display two columns, LastName and FirstName. How can that be done?

On the Form I have a ADOConnection1, an ADOTable1,an ADODataSource1 and a DBGrid1 with two undefined columns.
 
You need to change where you are declaring your variables and objects. I would make them part of your form's private declarations, rather than putting them in the Button's OnClick method.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top