Forum > Databases

[SOLVED] Adding and editing

(1/1)

Pe3s:
Hello, I wrote this code, how to correct the iID code to Integer variable or stay with the String variable?

Button Add

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TfGlowna.SpeedButton4Click(Sender: TObject);begin  fMiesiac.iID:='';  fMiesiac.ShowModal;end; 
Button edit

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TfGlowna.SpeedButton5Click(Sender: TObject);begin  fMiesiac.iID:= IntToStr(ZQuery1.FieldByName('MiesiacID').AsInteger);  fMiesiac.ComboBox1.Text:= ZQuery1.FieldByName('Miesiac').AsString;  fMiesiac.ShowModal;end; 
Form Add & edit

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit2; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Buttons; type   { TfMiesiac }   TfMiesiac = class(TForm)    BitBtn1: TBitBtn;    BitBtn2: TBitBtn;    ComboBox1: TComboBox;    Label1: TLabel;    procedure BitBtn1Click(Sender: TObject);  private   public    iID: String;   end; var  fMiesiac: TfMiesiac; implementation uses  Unit1; {$R *.lfm} { TfMiesiac } procedure TfMiesiac.BitBtn1Click(Sender: TObject);var  myID: Integer;begin  if iID='' then  begin    fGlowna.ZQuery1.Close;    fGlowna.ZQuery1.SQL.Clear;    fGlowna.ZQuery1.SQL.Text:= 'INSERT INTO miesiac(Miesiac) values(:Miesiac)';    fGlowna.ZQuery1.ParamByName('Miesiac').AsString:= ComboBox1.Items[ComboBox1.ItemIndex];  end else  begin    myID:= fGlowna.ZQuery1.FieldByName('MiesiacID').AsInteger;    fGlowna.ZQuery1.SQL.Clear;    fGlowna.ZQuery1.SQL.Text:= 'UPDATE miesiac SET Miesiac=:Miesiac WHERE MiesiacID=:MiesiacID';    fGlowna.ZQuery1.ParamByName('Miesiac').AsString:= ComboBox1.Items[ComboBox1.ItemIndex];    fGlowna.ZQuery1.ParamByName('MiesiacID').AsInteger:= myID;  end;   fGlowna.ZQuery1.ExecSQL;   fGlowna.ZQuery1.Close;  fGlowna.ZQuery1.SQL.Clear;  fGlowna.ZQuery1.SQL.Text:= 'SELECT * FROM miesiac';  fGlowna.ZQuery1.Open;  fGlowna.ZQuery1.Last;   Self.Close;end; end. 

paweld:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TfGlowna.SpeedButton4Click(Sender: TObject); //addbegin  fMiesiac.iID := 0;  fMiesiac.ShowModal;end; procedure TfGlowna.SpeedButton5Click(Sender: TObject); //editbegin  fMiesiac.iID := ZQuery1.FieldByName('MiesiacID').AsInteger;  fMiesiac.ComboBox1.Text := ZQuery1.FieldByName('Miesiac').AsString;  fMiesiac.ShowModal;end;Form2:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit2; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Buttons; type   { TfMiesiac }   TfMiesiac = class(TForm)    BitBtn1: TBitBtn;    BitBtn2: TBitBtn;    ComboBox1: TComboBox;    Label1: TLabel;    procedure BitBtn1Click(Sender: TObject);  private   public    iID: Integer;   end; var  fMiesiac: TfMiesiac; implementation uses  Unit1; {$R *.lfm} { TfMiesiac } procedure TfMiesiac.BitBtn1Click(Sender: TObject);begin  fGlowna.ZQuery2.SQL.Clear;  if iID < 1 then  //corrected typo  begin    fGlowna.ZQuery2.SQL.Text := 'INSERT INTO miesiac(Miesiac) values(:Miesiac)';  end   else  begin    fGlowna.ZQuery2.SQL.Text := 'UPDATE miesiac SET Miesiac=:Miesiac WHERE MiesiacID=:MiesiacID';    fGlowna.ZQuery2.ParamByName('MiesiacID').AsInteger:= iID;  end;  fGlowna.ZQuery2.ParamByName('Miesiac').AsString := ComboBox1.Items[ComboBox1.ItemIndex];  fGlowna.ZQuery2.ExecSQL;   fGlowna.ZQuery1.Refresh;  fGlowna.ZQuery1.Last;  Self.Close;end; end.Don't be afraid to use more than one TZQuery component. You won't have to unnecessarily close and duplicate the same queries every time

Pe3s:
The problem is that it does not add new records, only updates

paweld:
sorry for the typo. corrected previous post - form2, line 42.

Pe3s:
@paweld, Thank you :)

Navigation

[0] Message Index

Go to full version