Cau, mam na Vas takovou prosbu, nemuzu to nikde v knize,ani na netu najit ...
Mam form1, na kterym mam: 2 tlacitka (BtNova/BtUpravit)
1 DBGrid ................... DataSource := DataSource1
1 DataSource1 .......... DataSet:= Table1
1 Table1 .................... DatabaseName:= .\DB
.................. TableName:= Znamky.db
v tabulce mam 3 sloupce ... data1,data2,data3, .... vsechny string
form2, .......................... 1 query1 ................... DataSource:= Form1.Datasource1
.............. DataSet:= Table1
.............. DatabaseName:= .\DB
3 DBEdit .................. DataSource:= Form1.DataSource1
.......... DataField:= data1 ,2 ,3
1 tlacitko (BtUlozit)
Kod na stisk tlacitka: (BtNova/BtUpravit)
procedure TForm1.BtNovaClick(Sender: TObject);
begin
with tform2.Create(nil) do
try
datasource.dataset := table1;
operation := oInsert;
showmodal;
finally
release;
end;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
table1.Open;
end;
procedure TForm1.BtUpravitClick(Sender: TObject);
begin
with tform2.Create(nil) do
try
datasource.dataset := table1;
operation := oEdit;
showmodal;
finally
release;
end;
end;
Kod Form2:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtDlgs, DBXpress, FMTBcd, SqlExpr, DB,
Mask, DBCtrls, DBTables;
type TOperation = (oInsert, oEdit);
type
TForm2 = class(TForm)
DBEdit1: TDBEdit;
DBEdit2: TDBEdit;
DBEdit3: TDBEdit;
BtUlozit: TButton;
DataSource: TDataSource;
Query1: TQuery;
procedure BtUlozitClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
FOperation: Toperation;
public
{ Public declarations }
property Operation: TOperation read FOperation write FOperation;
end;
var
Form2: TForm2;
implementation
uses Unit1;
{$R *.dfm}
procedure TForm2.BtUlozitClick(Sender: TObject);
begin
modalresult := mrOK;
with Form1.Table1 do
begin
if not active then open;
Insert;
DBEdit1.Datasource.Dataset.FieldByName('Data1').AsString;// Pokud pole Data1 je varchar
Post;
end;
end;
procedure TForm2.FormShow(Sender: TObject);
var novahodnota: integer;
begin
if operation = oInsert then
begin
query1.SQL.Clear;
query1.SQL.add('select max(data1) from znamky.db');
query1.Open;
NovaHodnota := query1.Fields[0].asinteger + 1;
query1.Close;
datasource.DataSet.insert;
datasource.DataSet.FieldByName('data1').AsInteger := novahodnota;
end
else
datasource.DataSet.edit;
end;
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if modalresult = mrok then
datasource.DataSet.post
else
datasource.DataSet.Cancel;
end;
end.
V DBEdit1 se mi rovnou vypise cislo dalsi polozky a kdyz do DBEdit2 ,3 zadam nejakej text a stisknu ulozit, tak mi to
vypise chybu Table1: DataSet not in edit or insert mode.
Navic, kdyz (Ctrl+F2) a znovu spustim, tak se do DBEdit1 vypise cislo o 1 vetsi, nez minule, ale kdyz Zavru Delphi a znovu otevru, tak jakoby se do tabulky neulozilo nic, .. zacina to vse zase od zacatku.
Prosim poradi nekdo, co mam spatne ??