I am a beginner to the LCL. I wrote some sql-programs, which all include the same procedure:
procedure TForm1.sql_read(sql: ansistring);
{executes the sql-command 'sql' and reads all records into the dataset
'DBGrid1.DataSource.dataset'. If DBGrid1.Visible=True, the result is
shown in a Grid}
begin
try
SQLQuery1.Close; // sonst ist SQL-Befehl nicht änderbar
SQLQuery1.SQL.Text:=sql; // wanted SQL-Befehl
DBConnection.Connected:= True; // establish the connection to the DB
SQLTransaction1.Active:= True; // activate the transaction
SQLQuery1.Open; // open the SQL-query
SQLQuery1.Last; {puts the cursor at the last record in the dataset,
fetching all records from the underlying DB if needed}
except
on E: EDatabaseError do
begin
ShowMessage('DB-Error = ' + E.message);
end;
end; {try}
end; {sql_read}
and some more doubly procedures. I am looking for a better, re-usable way like a unit to collect all my common sql-procedures. I started with:
unit sqlunit;
{$mode objfpc}{$H+}
interface
uses sqlite3conn, sqldb, db, Forms, Dialogs, DBGrids;
type
TFormSQL = class(TForm)
DataSource1: TDataSource;
DBConnection: TSQLite3Connection;
DBGrid1: TDBGrid;
SQLQuery1: TSQLQuery;
SQLTransaction1: TSQLTransaction;
procedure sql_read(sql: ansistring);
private { private declarations }
public { public declarations }
end;
var FormSQL: TFormSQL;
implementation
{$R *.lfm}
procedure TFormSQL.sql_read(sql: ansistring);
{executes the sql-command 'sql' and reads all records into the dataset
'DBGrid1.DataSource.dataset'. If DBGrid1.Visible=True, the result is
shown in a Grid}
begin
try
SQLQuery1.Close; // sonst ist SQL-Befehl nicht änderbar
SQLQuery1.SQL.Text:=sql; // wanted SQL-Befehl
DBConnection.Connected:= True; // establish the connection to the DB
SQLTransaction1.Active:= True; // activate the transaction
SQLQuery1.Open; // open the SQL-query
SQLQuery1.Last; {puts the cursor at the last record in the dataset,
fetching all records from the underlying DB if needed}
except
on E: EDatabaseError do
begin
ShowMessage('DB-Error = ' + E.message);
end;
end; {try}
end; {sql_read}
end.
But from now I don't know how to continue, because now I have 2 Forms to handle (1 in unit sqlunit and 1 in my main unit) and don't know how to "connect" both and how to handle them via Object Inspector.
Procedure sql_read() shall display it's data in a TDBGrid, so I think type TFormSQL must be derived from class TForm. Is that correct?
How can the Object Inspector handle TFormSQL?
All the GUI-controls of my program need a second Form, because I cannot put them in my common sql-unit.
How must I create this second Form in my main unit? Must this main Form be a heritage of TFormSQL?
How can the Object Inspector handle this second Form?
Thanks very much in advance for your help.