Looking at the Details for an Invoicing Application in Delphi - Settings Unit
(Page 2 of 6 )

unit settings;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons,INIFiles;
type
TForm3 = class(TForm)
StaticText1: TStaticText;
btnsave: TBitBtn;
edVat: TEdit;
StaticText2: TStaticText;
Label1: TLabel;
btnexit: TBitBtn;
StaticText3: TStaticText;
edcon: TEdit;
StaticText4: TStaticText;
StaticText5: TStaticText;
Label2: TLabel;
procedure btnsaveClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnexitClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
uses invmain;
{$R *.dfm}
procedure TForm3.btnsaveClick(Sender: TObject);
var
settings : TINIFile;
begin
settings := TINIFile.Create(ExtractFilePath(Application.EXEName) + 'settings.ini');
try
settings.WriteFloat('Settings', 'vat', strtofloat(edvat.Text));
settings.WriteString('Settings', 'con', edcon.Text);
messagedlg('Your settings will take effect the next time you start the Invoice Manager.', mtinformation , [mbOK], 0);
finally
settings.Free;
close;
end;
end;
procedure TForm3.FormCreate(Sender: TObject);
begin
label1.caption:=floattostr(form1.vat);
label2.caption:=form1.con;
end;
procedure TForm3.btnexitClick(Sender: TObject);
begin
close;
end;
end.
Of these three values that are stored in the ini file, the one that is most critical to the application is the "con" variable. As I’ve stated already, the value stored within this variable is used by the application to connect to the database tables. The database connections are made after the form is created, and just before the form is shown. Below is the code responsible for doing this:
procedure TForm1.FormShow(Sender: TObject);
var
i:integer;
begin
//set ado properties
ado1.ConnectionString:=con;
ado1.TableName:='invoices';
ado1.Active:=true;
label1.caption:=inttostr(ado1.RecordCount);
i:=ado1.RecordCount;
label2.Caption:=inttostr(i);
end;
Currently the "con" variable contains the following path:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=inv.mdb;Persist Security Info=False
Within the above line you can clearly see the name of the database. I wanted to point that out before we discuss the FormShow procedure, because this procedure shows code that only loads the table, and of course anyone looking at the code would ask, "How does the application know which database to use?" The code starts by setting the ado1 table’s connection string to the path specified in the con variable and then (since it knows which database to use) it sets the tablename property of the ado table component and activates it. Label1 and label2 displays the total number of records (invoices) in the database and the invoice number of the the current location of the record pointer.
Next: New Invoice Form and Code >>
More Delphi-Kylix Articles
More By Chris Neeman