Questo è il codice presente nella form principale.
codice:
procedure Tprincipale.Refresh(Sender: TObject);
begin
DBConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' +
DbCorrente +
';Mode=ReadWrite|Share Deny None;Persist Security In' +
'fo=False';
if FileExists(DbCorrente) then
begin
try
DefaultConnection := TADOConnection.Create(self);
DefaultConnection.LoginPrompt := False;
DefaultConnection.ConnectionString := DBConnectionString;
if (DbAssente = False) and (DbCorrente <> '') then
begin
materie.Clear;
materie.Text := 'Scegli una materia...';
ADOQueryMaterie := TADOQuery.Create(self);
ADOQueryMaterie.Connection := DefaultConnection;
ADOQueryMaterie.SQL.Clear;
ADOQueryMaterie.SQL.Add('SELECT * FROM materie');
ADOQueryMaterie.Open;
while not ADOQueryMaterie.eof do
begin
Materie.Items.Add(ADOQueryMaterie.FieldValues['Nome']);
ADOQueryMaterie.Next;
end;
ADOQueryMaterie.Free;
end;
except
on e: Exception do
begin
if ErroreMostrato = False then
begin
ErroreMostrato := True;
reg.WriteBool('ErroreMostrato',True);
raise Exception.Create('Si è verificato un errore nell''accedere al registro "'+
DbCorrente+'"'+#13+#10+'Il registro in questione potrebbe essere '+
'stato danneggiato. Si consiglia di creare un registro nuovo.'+#13+#10+#13+#10+
'L''errore è il seguente: '+#13+#10+e.Message);
end;
end;
end;
end;
procedure Tprincipale.nuovamateriaClick(Sender: TObject);
begin
AggiungiMateria.Position := poMainFormCenter;
AggiungiMateria.ShowModal;
if AggiungiMateria.ModalResult = mrOK then Refresh(self); (* UNA VOLTA CHE IL FORM SEDCONDARIO
"AggiungiMateria" E' STATO CHIUSO CLICCANDO SU "OK",
AGGIORNA QUESTO FORM MEDIANTE LA FUNZIONE REFRESH *)
end;
E quest'altro è il codice presente nella form secondaria.
codice:
procedure TAggiungiMateria.okClick(Sender: TObject);
var NomeMateria : string;
var ID : string;
var Comando : string;
begin
Comando :=
'INSERT INTO materie (ID, Nome, VotoUnico) VALUES' +
' ( ' +
' "' + ID + '", ' +
' "' + materia.Text + '", ' +
' "0"' +
' ) ';
if [CONDIZIONE] then
begin
ADOCommand := TADOCommand.Create(self);
ADOCommand.Connection := DefaultConnection;
ADOCommand.CommandText := Comando;
ADOCommand.Execute;
ADOCommand.Free;
end
else [ECC...]
end;