Tweet RSS

Example Delphi Field by Number Code:

Div
unit DBU ;//DBU - Database Unit
  //Jeremy Paton
  //Field by Number [loop]
interface

uses DB, ADODB, forms;

var MyDB : TADOQuery;

procedure opendb;
Function Query(TSQL: String): String;
Procedure change(TSQL: String);

implementation

procedure opendb;
begin
  MyDB := TADOQuery.Create(Application);
  MyDB.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' +
    '"MyFriends.mdb";Persist Security Info=False'; //Used to link to MS access database
end;


Procedure change(TSQL: String);
begin
  begin
    MyDB.Close;
    MyDB.SQL.Text:= TSQL;
    MyDB.ExecSQL;
  end;
end;


Function Query(TSQL: String): String;
Var
  temp: String;
  Loop: Integer;
begin
  temp:='';
    Change(TSQL);  //Essential before a new SQL statement is used. Closes the Data-Set
    MyDb.Open;  //Open Data-Set so you can use it.
    MyDb.First; //Internal pointer set to first data-set.
    While not MyDB.Eof do //EOF -End of File.
    Begin //Extracting each field, by heading.
      For loop:= 1 to MyDB.FieldCount do
        Temp:= Temp+MyDB.Fields.FieldByNumber(loop).AsString+#9;
      MyDb.Next;
      Temp:= temp+#13
    End;
  Result:= temp;
end;

end.

Linking Databases to Delphi:

<< Previous Tutorial

>> Next Tutorial

Div

Current Tutorial:

</> Download Source Code