Looking at the Details for an Invoicing Application in Delphi
In the previous article we discussed the requirements and purpose of the invoicing application that we are creating. We also listed the main application code. In this part of the article we are going to look at how to connect the database that we created earlier to the application code. We will also look at some of the code in detail.
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:
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.