Recent

Author Topic: [SOLVED] Adding and editing  (Read 248 times)

Pe3s

  • Sr. Member
  • ****
  • Posts: 330
[SOLVED] Adding and editing
« on: November 26, 2022, 06:33:39 pm »
Hello, I wrote this code, how to correct the iID code to Integer variable or stay with the String variable?

Button Add
Code: Pascal  [Select][+][-]
  1. procedure TfGlowna.SpeedButton4Click(Sender: TObject);
  2. begin
  3.   fMiesiac.iID:='';
  4.   fMiesiac.ShowModal;
  5. end;
  6.  

Button edit
Code: Pascal  [Select][+][-]
  1. procedure TfGlowna.SpeedButton5Click(Sender: TObject);
  2. begin
  3.   fMiesiac.iID:= IntToStr(ZQuery1.FieldByName('MiesiacID').AsInteger);
  4.   fMiesiac.ComboBox1.Text:= ZQuery1.FieldByName('Miesiac').AsString;
  5.   fMiesiac.ShowModal;
  6. end;
  7.  

Form Add & edit
Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Buttons;
  9.  
  10. type
  11.  
  12.   { TfMiesiac }
  13.  
  14.   TfMiesiac = class(TForm)
  15.     BitBtn1: TBitBtn;
  16.     BitBtn2: TBitBtn;
  17.     ComboBox1: TComboBox;
  18.     Label1: TLabel;
  19.     procedure BitBtn1Click(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.     iID: String;
  24.  
  25.   end;
  26.  
  27. var
  28.   fMiesiac: TfMiesiac;
  29.  
  30. implementation
  31.  
  32. uses
  33.   Unit1;
  34.  
  35. {$R *.lfm}
  36.  
  37. { TfMiesiac }
  38.  
  39. procedure TfMiesiac.BitBtn1Click(Sender: TObject);
  40. var
  41.   myID: Integer;
  42. begin
  43.   if iID='' then
  44.   begin
  45.     fGlowna.ZQuery1.Close;
  46.     fGlowna.ZQuery1.SQL.Clear;
  47.     fGlowna.ZQuery1.SQL.Text:= 'INSERT INTO miesiac(Miesiac) values(:Miesiac)';
  48.     fGlowna.ZQuery1.ParamByName('Miesiac').AsString:= ComboBox1.Items[ComboBox1.ItemIndex];
  49.   end else
  50.   begin
  51.     myID:= fGlowna.ZQuery1.FieldByName('MiesiacID').AsInteger;
  52.     fGlowna.ZQuery1.SQL.Clear;
  53.     fGlowna.ZQuery1.SQL.Text:= 'UPDATE miesiac SET Miesiac=:Miesiac WHERE MiesiacID=:MiesiacID';
  54.     fGlowna.ZQuery1.ParamByName('Miesiac').AsString:= ComboBox1.Items[ComboBox1.ItemIndex];
  55.     fGlowna.ZQuery1.ParamByName('MiesiacID').AsInteger:= myID;
  56.   end;
  57.  
  58.   fGlowna.ZQuery1.ExecSQL;
  59.  
  60.   fGlowna.ZQuery1.Close;
  61.   fGlowna.ZQuery1.SQL.Clear;
  62.   fGlowna.ZQuery1.SQL.Text:= 'SELECT * FROM miesiac';
  63.   fGlowna.ZQuery1.Open;
  64.   fGlowna.ZQuery1.Last;
  65.  
  66.   Self.Close;
  67. end;
  68.  
  69. end.
  70.  
« Last Edit: November 26, 2022, 08:47:49 pm by Pe3s »

paweld

  • Hero Member
  • *****
  • Posts: 556
Re: Adding and editing
« Reply #1 on: November 26, 2022, 07:53:30 pm »
Code: Pascal  [Select][+][-]
  1. procedure TfGlowna.SpeedButton4Click(Sender: TObject); //add
  2. begin
  3.   fMiesiac.iID := 0;
  4.   fMiesiac.ShowModal;
  5. end;
  6.  
  7. procedure TfGlowna.SpeedButton5Click(Sender: TObject); //edit
  8. begin
  9.   fMiesiac.iID := ZQuery1.FieldByName('MiesiacID').AsInteger;
  10.   fMiesiac.ComboBox1.Text := ZQuery1.FieldByName('Miesiac').AsString;
  11.   fMiesiac.ShowModal;
  12. end;
Form2:
Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Buttons;
  9.  
  10. type
  11.  
  12.   { TfMiesiac }
  13.  
  14.   TfMiesiac = class(TForm)
  15.     BitBtn1: TBitBtn;
  16.     BitBtn2: TBitBtn;
  17.     ComboBox1: TComboBox;
  18.     Label1: TLabel;
  19.     procedure BitBtn1Click(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.     iID: Integer;
  24.  
  25.   end;
  26.  
  27. var
  28.   fMiesiac: TfMiesiac;
  29.  
  30. implementation
  31.  
  32. uses
  33.   Unit1;
  34.  
  35. {$R *.lfm}
  36.  
  37. { TfMiesiac }
  38.  
  39. procedure TfMiesiac.BitBtn1Click(Sender: TObject);
  40. begin
  41.   fGlowna.ZQuery2.SQL.Clear;
  42.   if iID < 1 then  //corrected typo
  43.   begin
  44.     fGlowna.ZQuery2.SQL.Text := 'INSERT INTO miesiac(Miesiac) values(:Miesiac)';
  45.   end
  46.   else
  47.   begin
  48.     fGlowna.ZQuery2.SQL.Text := 'UPDATE miesiac SET Miesiac=:Miesiac WHERE MiesiacID=:MiesiacID';
  49.     fGlowna.ZQuery2.ParamByName('MiesiacID').AsInteger:= iID;
  50.   end;
  51.   fGlowna.ZQuery2.ParamByName('Miesiac').AsString := ComboBox1.Items[ComboBox1.ItemIndex];
  52.   fGlowna.ZQuery2.ExecSQL;
  53.  
  54.   fGlowna.ZQuery1.Refresh;
  55.   fGlowna.ZQuery1.Last;
  56.   Self.Close;
  57. end;
  58.  
  59. 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
« Last Edit: November 26, 2022, 08:35:22 pm by paweld »
Best regards / Pozdrawiam
paweld

Pe3s

  • Sr. Member
  • ****
  • Posts: 330
Re: Adding and editing
« Reply #2 on: November 26, 2022, 08:26:46 pm »
The problem is that it does not add new records, only updates

paweld

  • Hero Member
  • *****
  • Posts: 556
Re: Adding and editing
« Reply #3 on: November 26, 2022, 08:35:49 pm »
sorry for the typo. corrected previous post - form2, line 42.
Best regards / Pozdrawiam
paweld

Pe3s

  • Sr. Member
  • ****
  • Posts: 330
Re: Adding and editing
« Reply #4 on: November 26, 2022, 08:47:36 pm »
@paweld, Thank you :)

 

TinyPortal © 2005-2018