Recent

Author Topic: dbgrid and dbf  (Read 3390 times)

Fabius

  • Jr. Member
  • **
  • Posts: 59
dbgrid and dbf
« on: June 01, 2021, 06:17:04 pm »
Hello everyone, thanks for reading me, lol

I'm from France, I speak poor English, sorry

I have the following problem

I have a dbgrid which linked to a Dbase table, by Dbf and datasource

I fill in the DBF by an import procedure from a bank OFX

the content of the DBF appears correctly in the dbgrid,

but when i click the elevator or browse the dbgrid, all records become the same

Where do you think it came from, a bug, bad programming?

Thank you for your answers :(

cordially ;)

Fabius

  • Jr. Member
  • **
  • Posts: 59
Re: dbgrid and dbf
« Reply #1 on: June 01, 2021, 06:23:25 pm »
photo

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: dbgrid and dbf
« Reply #2 on: June 01, 2021, 06:29:12 pm »
Hello Fabius,
Welcome to the forum.

I wrote a small stock control program for used in a small shop, using TDBF, TDBGrid and many other components. The program was written using Lazarus 0.9.26, many years have passed so far no complain from the user. So I believe, you must have done something wrong.

We need more information. It would be better if you can provide a compile-able source code. If you are not willing to publicize the project, you can write a short demo that can show us the issue.

Your English is good. Very clear and understandable.

Fabius

  • Jr. Member
  • **
  • Posts: 59
Re: dbgrid and dbf
« Reply #3 on: June 02, 2021, 09:37:05 am »
Hello, thank you for the reply
I have also written several softwares that use DBF tables, with Dbase and a simple one with Lazarus

I believe that the DBF tables are stable, my problem is not the table

which opened with Dbase is with the correct records

My worry is that when I click on the srollbar

the content of the dbgrid is not stable, the good records disappear from the dbgrid which displays a repeating line

the table is OK, I visualize the content with Dbase 1702 ...

I must indeed be wrong, but where?

I give you the code *. not of the two files





This code it OK , but the problem are srollbar ???

I don't think it's lazarus ...... I have to do something bad, but where ??

Thank you for your help


Fabius

  • Jr. Member
  • **
  • Posts: 59
Re: dbgrid and dbf
« Reply #4 on: June 02, 2021, 09:37:26 am »
Code: Pascal  [Select][+][-]
  1. #
  2. unit importation;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, dbf, DB, Forms, Controls, Graphics, Dialogs, StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm7 }
  14.  
  15.   TForm7 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     Edit1: TEdit;
  19.     Label1: TLabel;
  20.     Label2: TLabel;
  21.     Label3: TLabel;
  22.     Memo1: TMemo;
  23.     Memo2: TMemo;
  24.     OpenDialog1: TOpenDialog;
  25.     procedure Button1Click(Sender: TObject);
  26.     procedure Button2Click(Sender: TObject);
  27.   private
  28.  
  29.   public
  30.  
  31.   end;
  32.  
  33. var
  34.   Form7: TForm7;
  35.  
  36. implementation
  37. uses Unit3;
  38.  
  39. {$R *.lfm}
  40.  
  41. { TForm7 }
  42.  
  43. procedure TForm7.Button1Click(Sender: TObject);
  44. Var i:integer;
  45. Var Nnnn:integer;
  46. begin
  47.   if OpenDialog1.Execute then
  48.     begin
  49.     Memo1.Lines.Clear;
  50.     Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
  51.  
  52.     /////////////supprimer les lignes du début
  53.     (* commenté
  54. SECURITY:NONE
  55. ENCODING:USASCII
  56. CHARSET:1252
  57. COMPRESSION:NONE
  58. OLDFILEUID:NONE
  59. NEWFILEUID:NONE
  60. <OFX>
  61. <SIGNONMSGSRSV1>
  62. <SONRS>
  63. <STATUS>
  64. <CODE>0
  65. <SEVERITY>INFO
  66. </STATUS>
  67. <DTSERVER>20210526154523
  68. <LANGUAGE>FRA
  69. </SONRS>
  70. </SIGNONMSGSRSV1>
  71. <BANKMSGSRSV1>
  72. <STMTTRNRS>
  73. <TRNUID>96746354948
  74. <STATUS>
  75. <CODE>0
  76. <SEVERITY>INFO
  77. </STATUS>
  78. <STMTRS>
  79. <CURDEF>EUR
  80. <BANKACCTFROM>
  81. <BANKID>18106
  82. <BRANCHID>00038
  83. <ACCTID>96746354948
  84. <ACCTTYPE>CHECKING
  85. </BANKACCTFROM>
  86. <BANKTRANLIST>
  87. <DTSTART>20200501000000
  88. <DTEND>20210525235959
  89. <STMTTRN>
  90. <TRNTYPE>OTHER
  91.  
  92. </BANKTRANLIST>
  93. <LEDGERBAL>
  94. <BALAMT>+12191.25
  95. <DTASOF>20210526
  96. </LEDGERBAL>
  97. <AVAILBAL>
  98. <BALAMT>+12191.25
  99. <DTASOF>20210526
  100. </AVAILBAL>
  101. </STMTRS>
  102. </STMTTRNRS>
  103. </BANKMSGSRSV1>
  104. </OFX>
  105. *)
  106.  
  107.  
  108.     Memo1.Refresh;
  109.     Memo1.SetFocus;
  110.     Memo1.SelStart:=0;
  111.     Memo1.Refresh;
  112.       i:=0;
  113.       ///////////Memo1.Lines.Count-1
  114.       (*
  115.       Memo1.Lines[0];
  116.       for i := 0 to 80 do
  117.       begin
  118.       Memo1.Lines.Delete(i);
  119.       end;
  120.       *)
  121.     Memo1.SetFocus;
  122.     Memo1.SelStart:=0;
  123.     Edit1.Text := OpenDialog1.FileName;
  124.     Edit1.Visible:= True;
  125.  
  126.     Label2.Caption:= 'Fichier en Cours : ' + IntToStr(Memo1.Lines.Count) + ' Lignes';
  127.     Label2.Visible:= True;
  128.  
  129.     end;
  130. end;
  131.  
  132. procedure TForm7.Button2Click(Sender: TObject);
  133. ////////OFX
  134. Var i:integer;
  135. Var NombreT:integer;
  136. Var NomFichierEnCours : String;
  137. Var DateRow : String;
  138. Var DescriptionMouvement : String;
  139. Var Montant:String;
  140. Var MontantCredit : String;
  141. Var MontantDebit : String;
  142. Var MontantDebitNum : extended;
  143. Var MontantCreditNum : extended;
  144.  
  145. Var fichier: TStringList;
  146. Var elements: String;
  147. Var buttonSelected : Integer;
  148. Var Ligness : Integer;
  149. Var LigneValueFid : String;
  150. Var LigneValueTravail : String;
  151. Var LigneValueMemo : String;
  152. Var LigneSTMTTRN : String;
  153. Var LigneSTMTTRN2 : String;
  154. Var LigneTRNTYPE  : String;
  155.  
  156. begin
  157.  
  158.   Form3.Dbf1.Active:= True;
  159.   Form3.Dbf1.First;
  160.  
  161.    ///////////tester si la table de saisie à des enregistrements
  162.   if Form3.Dbf1.RecordCount > 0 Then
  163.   begin
  164.    messagedlg('La Table de destination n est pas vide .',mtinformation,[mbok],0);
  165.  abort;
  166.   end;
  167.          //////////// enregistrement sur 9 lignes
  168. ///<DTPOSTED>20210525
  169. ///<DTAVAIL>20210525
  170. ///<TRNAMT>-0.23
  171. ///<FITID>6230510097758
  172. ///<NAME>VIREMENT UNITAIR N°2114500108875
  173. ///<MEMO>.
  174. ///</STMTTRN>
  175. ///<STMTTRN>
  176. ///<TRNTYPE>OTHER
  177.  
  178.   ////////////////avertir
  179.   buttonSelected := MessageDlg('Lancer importation ? ',mtCustom,
  180.                               [mbYes,mbCancel], 0);
  181.  
  182.   if buttonSelected = mrYes    then
  183.   begin
  184.  
  185.   Ligness:=0;
  186.   NombreT := 0;
  187.  
  188.   Form3.Dbf1.IndexName:= 'id';
  189.       Memo1.SetFocus;
  190.     Memo1.SelStart:=0;
  191.   for i := 0 to Memo1.Lines.Count-1 do
  192.     begin
  193.  
  194.  
  195.       if (Memo1.Lines.Strings[i] <> '') then
  196.       begin
  197.  
  198.       ///////////// on démarre un enregistrement
  199.            if Pos ('<STMTTRN>', Memo1.Lines.Strings[i]) > 0 then  ////// 1 on débute un nouvel enregistrement avec la date
  200.            begin
  201.            Ligness := 0;
  202.            Montant := '';
  203.            DescriptionMouvement := '';
  204.            DateRow := '';
  205.            Memo2.Append('Debut');
  206.            MontantCreditNum := 0;
  207.            MontantDebitNum :=0;
  208.            MontantDebit :='';
  209.            MontantCredit := '';
  210.            end;
  211.  
  212.  
  213.                       ///<TRNTYPE>OTHER
  214.            if Pos ('<TRNTYPE>', Memo1.Lines.Strings[i]) > 0 then   //9
  215.            begin
  216.            Ligness := Ligness+1;
  217.            end;
  218.  
  219.  
  220.            //<DTPOSTED>20210525
  221.            if Pos ('<DTPOSTED>', Memo1.Lines.Strings[i]) > 0 then  ////// nouvel enregistrement avec la date  juste apres <STMTTRN>
  222.            begin
  223.            Ligness := Ligness+1;
  224.            DateRow := copy(Memo1.Lines.Strings[i], 11, 30);
  225.            Memo2.Append(DateRow);
  226.            end;
  227.  
  228.            ///<DTAVAIL>20210525
  229.            if Pos ('<DTAVAIL>', Memo1.Lines.Strings[i]) > 0 then   ///2
  230.            begin
  231.            Ligness := Ligness+1;
  232.            LigneValueTravail := copy(Memo1.Lines.Strings[i], 10, 30);
  233.            Memo2.Append(LigneValueTravail);
  234.            end;
  235.  
  236.            if Pos ('<TRNAMT>', Memo1.Lines.Strings[i]) > 0 then  //////le montant 3
  237.            begin
  238.            Montant := copy(Memo1.Lines.Strings[i], 9, 20);
  239.            Ligness := Ligness+1;
  240.  
  241.                 if Pos ('+', Montant) > 0 then  //////le montant  est positif CREDIT
  242.                 begin
  243.                   MontantCredit := copy(Montant, 2, 20);
  244.                   Memo2.Append(MontantCredit);
  245.                   MontantCredit := MontantCredit.Replace('.', ',');
  246.                   MontantCreditNum :=  StrToFloat(MontantCredit);
  247.                 end else
  248.                 begin  ////le montant est un DEBIT
  249.                   MontantDebit := copy(Montant, 2, 20);
  250.                   Memo2.Append(MontantDebit);
  251.                   MontantDebit := MontantDebit.Replace('.', ',');
  252.                   MontantDebitNum := StrToFloat(MontantDebit);
  253.                 end;
  254.  
  255.             end;
  256.  
  257.  
  258.            ///<FITID>6230510097758
  259.            if Pos ('<FITID>', Memo1.Lines.Strings[i]) > 0 then    ////4
  260.            begin
  261.            Ligness := Ligness+1;
  262.             LigneValueFid := copy(Memo1.Lines.Strings[i], 8, 30);
  263.            Memo2.Append( LigneValueFid);
  264.            end;
  265.  
  266.            if Pos ('<NAME>', Memo1.Lines.Strings[i]) > 0 then  //////le descriptif   5
  267.            begin
  268.            Ligness := Ligness+1;
  269.            DescriptionMouvement := copy(Memo1.Lines.Strings[i], 7, 100);
  270.            Memo2.Append(DescriptionMouvement);
  271.            end;
  272.  
  273.            ///<MEMO>.
  274.            if Pos ('<MEMO>', Memo1.Lines.Strings[i]) > 0 then  ///6
  275.            begin
  276.            Ligness := Ligness+1;
  277.            LigneValueMemo:= copy(Memo1.Lines.Strings[i], 0, 100);  ////////////la totalité
  278.            Memo2.Append(LigneValueMemo);
  279.            end;
  280.  
  281.                     ///</STMTTRN>  noter le /  c la fin d un enregistrement
  282.            if  Pos('</STMTTRN>', Memo1.Lines.Strings[i]) > 0  then   ///7
  283.            begin
  284.                 if (Ligness = 7) Then
  285.                  begin
  286.                    Memo2.Append('Fin');
  287.  
  288.                    NombreT := NombreT + 1;
  289.  
  290.                    Ligness:=0;
  291.  
  292.                    Form3.Dbf1.Last;
  293.                    Form3.Dbf1.Append;
  294.                    Form3.Dbf1.FieldByName('compte').AsString := 'X';
  295.                    Form3.Dbf1.FieldByName('label').AsString := DescriptionMouvement;
  296.  
  297.                    if MontantCreditNum > 0 then
  298.                    begin
  299.                         Form3.Dbf1.FieldByName('credit').asfloat := MontantCreditNum;
  300.                         Form3.Dbf1.FieldByName('debit').AsFloat :=0;
  301.                    end;
  302.  
  303.                    if MontantDebitNum > 0 then
  304.                    begin
  305.                         Form3.Dbf1.FieldByName('debit').AsFloat := MontantDebitNum;
  306.                         Form3.Dbf1.FieldByName('credit').AsFloat :=0;
  307.                    end;
  308.  
  309.                    Form3.Dbf1.Post;
  310.                    Form3.Dbf1.Refresh;
  311.                    end;
  312.  
  313.  
  314.  
  315.  
  316.  
  317.            end;   /////////////FIn </STMTTRN>
  318.  
  319.  
  320.  
  321.            end;  //////////// fin de test ligne blanche
  322.  
  323.       Application.ProcessMessages;
  324.  
  325.       end;
  326.  
  327.           Form3.Dbf1.Refresh;
  328.  
  329.         if (NombreT > 0) Then
  330.         begin
  331.           Label3.Caption:= 'Nombre ' + IntToStr(NombreT);
  332.           Label3.Visible:= True;
  333.         end;
  334.  
  335.   end;
  336.    end;
  337.  
  338.  
  339.  
  340.  
  341. end.
  342.  
« Last Edit: June 02, 2021, 05:30:37 pm by Fabius »

Fabius

  • Jr. Member
  • **
  • Posts: 59
Re: dbgrid and dbf
« Reply #5 on: June 02, 2021, 09:38:05 am »
Code: Pascal  [Select][+][-]
  1. unit Unit3;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, DB, dbf, memds, Forms, Controls, Graphics, Dialogs,
  9.   StdCtrls, ComCtrls, DBGrids, Grids, DBCtrls, Spin, ExtCtrls, Buttons,
  10.   RTTIGrids, SpinEx;
  11.  
  12. type
  13.  
  14.   { TForm3 }
  15.  
  16.   TForm3 = class(TForm)
  17.     Bevel1: TBevel;
  18.     Bevel2: TBevel;
  19.     Bevel3: TBevel;
  20.     Button1: TButton;
  21.     Button2: TButton;
  22.     Button3: TButton;
  23.     Button4: TButton;
  24.     Button5: TButton;
  25.     Button6: TButton;
  26.     Button7: TButton;
  27.     Button8: TButton;
  28.     Button9: TButton;
  29.     ControlBar1: TControlBar;
  30.     DataSource1: TDataSource;
  31.     DataSource2: TDataSource;
  32.     DataSource3: TDataSource;
  33.     DataSource4: TDataSource;
  34.     DataSource5: TDataSource;
  35.     DataSource6: TDataSource;
  36.     DataSource7: TDataSource;
  37.     DBEdit1: TDBEdit;
  38.     DBEdit2: TDBEdit;
  39.     Dbf1: TDbf;
  40.     Dbf2: TDbf;
  41.     Dbf3: TDbf;
  42.     Dbf4: TDbf;
  43.     Dbf5: TDbf;
  44.     Dbf6: TDbf;
  45.     DBGrid2: TDBGrid;
  46.     DBGrid3: TDBGrid;
  47.     DBLookupComboBox1: TDBLookupComboBox;
  48.     DBLookupComboBox2: TDBLookupComboBox;
  49.     Edit1: TEdit;
  50.     Edit2: TEdit;
  51.     Edit3: TEdit;
  52.     CodeMouvement: TEdit;
  53.     Edit4: TEdit;
  54.     FloatSpinEditEx1: TFloatSpinEditEx;
  55.     FloatSpinEditEx2: TFloatSpinEditEx;
  56.     Label2: TLabel;
  57.     Label3: TLabel;
  58.     Label4: TLabel;
  59.     Label5: TLabel;
  60.     Label6: TLabel;
  61.     Label7: TLabel;
  62.     MemDataset1: TMemDataset;
  63.     Tdebit: TEdit;
  64.     Tcredit: TEdit;
  65.     Tdbfcompte: TDbf;
  66.     DBGrid1: TDBGrid;
  67.     Label1: TLabel;
  68.     StatusBar1: TStatusBar;
  69.     procedure Button1Click(Sender: TObject);
  70.     procedure Button2Click(Sender: TObject);
  71.     procedure Button3Click(Sender: TObject);
  72.     procedure Button4Click(Sender: TObject);
  73.     procedure Button5Click(Sender: TObject);
  74.     procedure Button6Click(Sender: TObject);
  75.     procedure Button7Click(Sender: TObject);
  76.     procedure Button8Click(Sender: TObject);
  77.     procedure Button9Click(Sender: TObject);
  78.     procedure DataSource1DataChange(Sender: TObject; Field: TField);
  79.     procedure DBComboBox2Change(Sender: TObject);
  80.     procedure Dbf1BeforeOpen(DataSet: TDataSet);
  81.    
  82.  
  83.     procedure DBGrid1ColExit(Sender: TObject);
  84.     procedure DBGrid1DblClick(Sender: TObject);
  85.     procedure DBGrid2Enter(Sender: TObject);
  86.     procedure DBGrid3DblClick(Sender: TObject);
  87.     procedure DBLookupComboBox1Change(Sender: TObject);
  88.     procedure Edit2Change(Sender: TObject);
  89.     procedure Edit2Exit(Sender: TObject);
  90.     procedure FloatSpinEditEx1Exit(Sender: TObject);
  91.     procedure FloatSpinEditEx2Change(Sender: TObject);
  92.     procedure FloatSpinEditEx2Exit(Sender: TObject);
  93.     procedure FormActivate(Sender: TObject);
  94.     procedure FormCreate(Sender: TObject);
  95.     procedure FormShow(Sender: TObject);
  96.  
  97.     procedure calcullignes;
  98.   private
  99.  
  100.   public
  101.  
  102.   end;
  103.  
  104. var
  105.   Form3: TForm3;
  106.  
  107. implementation
  108. uses Unit5 , Dbf_Common,dbf_dbffile,dbf_parser,dbf_prsdef,dbf_cursor,dbf_fields,dbf_pgfile,dbf_idxfile;
  109. {$R *.lfm}
  110.  
  111. { TForm3 }
  112.  
  113.  
  114.  
  115.  
  116. procedure TForm3.FormShow(Sender: TObject);
  117. begin
  118.   ////////
  119.     Form3.Top:= 100;
  120.     Form3.Left:= 100;
  121.           StatusBar1.SimpleText:= StatusBar1.Caption +' Calcul  ' ;
  122.  
  123.       Dbf1.Refresh;
  124.   DBGrid1.Refresh;
  125.  
  126.       calcullignes;
  127. end;
  128.  
  129. procedure TForm3.DBGrid1ColExit(Sender: TObject);
  130. var Xcompte : string;
  131. var Xlabel : string;
  132.  
  133. begin
  134.  
  135.      if (DBGrid1.SelectedField.FieldName = 'DEBIT') OR (DBGrid1.SelectedField.FieldName = 'CREDIT') then
  136.       begin
  137.       StatusBar1.SimpleText:= StatusBar1.Caption +' Calcul  ' ;
  138.       calcullignes;
  139.       end;
  140.  
  141.  
  142.  
  143.  
  144.     if DBGrid1.SelectedField.FieldName = 'COMPTE' then
  145.     begin
  146.       StatusBar1.SimpleText:= 'Recherche : ';
  147.       if (DBGrid1.Columns[0].Field.IsNull) then
  148.       begin
  149.  
  150.       end else begin
  151.       if (DBGrid1.Columns[0].Field.Value <> '') OR (DBGrid1.Columns[0].Field.Value <> 'Nil') then
  152.       begin
  153.       Xcompte := DBGrid1.Columns[0].Field.Value;
  154.       end else begin
  155.       Xcompte := '';
  156.       end;
  157.       end;
  158.  
  159.                          //////////recherche dans la table compte , si le compte saisie existe
  160.  
  161.       if (Xcompte <> '') then
  162.       begin
  163.  
  164.       Tdbfcompte.IndexName := 'compte';    ////table des comptes sur index compte
  165.       if Tdbfcompte.Locate('compte', Xcompte, [loPartialKey, loCaseInsensitive]) then
  166.       begin
  167.           ////////////liste les champs de la table sur l enregistrement
  168.           Button1.Enabled:= True ;
  169.           StatusBar1.SimpleText:= StatusBar1.Caption +' Trouve Ok ' ;
  170.  
  171.           Dbf1.Edit;
  172.           Dbf1.FieldByName('compte').AsString := Tdbfcompte.FieldByName('compte').AsString;
  173.           Dbf1.FieldByName('label').AsString := Tdbfcompte.FieldByName('label').AsString;
  174.           Dbf1.Post;
  175.  
  176.         Xlabel := Tdbfcompte.FieldByName('label').AsString;
  177.  
  178.         StatusBar1.SimpleText:= StatusBar1.Caption +' Ajouté ' + Xcompte + ' ' + Xlabel;
  179.         end else begin
  180.         Button1.Enabled:= False ;
  181.         StatusBar1.SimpleText := 'Non Trouvé : ' + Xcompte;
  182.         DBGrid1.SetFocus;
  183.         DBGrid1.SelectedColumn.FieldName := 'compte';
  184.         /////////Tdbfcompte. ['compte']. SetFocus;
  185.         end;
  186.     end;
  187.  
  188.      end else begin
  189.       StatusBar1.SimpleText := 'Error Vide : ';
  190.      end;
  191. end;
  192.  
  193. procedure TForm3.DBGrid1DblClick(Sender: TObject);
  194. begin
  195.   DBGrid1.Refresh;
  196. end;
  197.  
  198. procedure TForm3.DBGrid2Enter(Sender: TObject);
  199. Var Rcherche:String;
  200. Var Rliasse:String;
  201. begin
  202.   ///// le dbgrill des compte recois le focus on affiche la liasse auquel est attaché le compte
  203.      if Tdbfcompte.FieldByName('compte').AsString <> '' then
  204.      begin
  205.         Rcherche := Tdbfcompte.FieldByName('compte').AsString;
  206.         Tdbfcompte.IndexName := 'compte';
  207.         if Tdbfcompte.SearchKeyPChar(PAnsiChar(AnsiString(Rcherche)), stGreaterEqual) then
  208.         begin
  209.          Rliasse := Tdbfcompte.FieldByName('liasse').AsString;
  210.          /////pointeur sur le liasse correspondante
  211.          Dbf6.IndexName := 'liasse';
  212.          if Dbf6.SearchKeyPChar(PAnsiChar(AnsiString(Rliasse)), stGreaterEqual) then
  213.          begin
  214.          Dbf6.Refresh;
  215.          end;
  216.  
  217.         end;
  218.     end;
  219. end;
  220.  
  221. procedure TForm3.DBGrid3DblClick(Sender: TObject);
  222. Var FFF : String;
  223. begin
  224.   ////////////double clic sur liasse , on affiche les comptes associés
  225.  
  226.       if (DBEdit2.Text <> '') OR (DBEdit2.Text <> 'Nil') then
  227.       begin
  228.        //////////////appliquer filtre sur les comptes
  229.       FFF := DBEdit2.Text;
  230.       /////Tdbfcompte.IndexName:= 'liasse';
  231.       Tdbfcompte.Filter := 'liasse='+ QuotedStr(FFF);
  232.       Tdbfcompte.Filtered := true;
  233.       end;
  234.  
  235. //   Tdbfcompte
  236. end;
  237.  
  238. procedure TForm3.DBLookupComboBox1Change(Sender: TObject);
  239. begin
  240.   Edit4.Text:= DBLookupComboBox1.Text;
  241.   if Dbf1.RecordCount = 0 then
  242.   begin
  243.   Edit4.Color:=clblue;
  244.   end else begin
  245.    Edit4.Color:=clGreen;
  246.   end;
  247. end;
  248.  
  249. procedure TForm3.Edit2Change(Sender: TObject);
  250. Var Rrrrr : String;
  251. begin
  252.   ////////////////champ de saissie  compte , il change on change la liste des comptes
  253.     Rrrrr := Edit2.Text;
  254.     Tdbfcompte.IndexName:= 'compte';
  255.     if Tdbfcompte.Locate('compte', Rrrrr, [loPartialKey, loCaseInsensitive]) then
  256.     begin
  257.     Tdbfcompte.Refresh;
  258.     Edit3.Text := Tdbfcompte.FieldByName('label').AsString;
  259.     end;
  260.  
  261. end;
  262.  
  263. procedure TForm3.Edit2Exit(Sender: TObject);
  264. begin
  265.    Edit3.SetFocus;
  266. end;
  267.  
  268. procedure TForm3.FloatSpinEditEx1Exit(Sender: TObject);
  269. begin
  270.    Button8.SetFocus;
  271.  
  272. end;
  273.  
  274. procedure TForm3.FloatSpinEditEx2Change(Sender: TObject);
  275. begin
  276.  
  277. end;
  278.  
  279. procedure TForm3.FloatSpinEditEx2Exit(Sender: TObject);
  280. begin
  281.   FloatSpinEditEx1.SetFocus;
  282. end;
  283.  
  284. procedure TForm3.FormActivate(Sender: TObject);
  285. begin
  286.   Dbf1.Refresh;
  287.   DBGrid1.Refresh;
  288.   DBGrid1.SetFocus;
  289.   DBGrid1.Update;
  290.   calcullignes;
  291. end;
  292.  
  293. procedure TForm3.FormCreate(Sender: TObject);
  294. begin
  295.   ////// les chemins pour les tables
  296.  
  297.   ///       CodeMouvement.Color:=;
  298.  
  299.  
  300.   Dbf1.First;
  301.   if Dbf1.RecordCount = 0 then
  302.   begin
  303.        Edit4.Color:=clblue;
  304.   end else begin
  305.      if Edit4.Text <> '' then
  306.      begin
  307.        Edit4.Color:=clGreen;
  308.        end else begin
  309.        Edit4.Color:=clblue;
  310.      end;
  311.   end;
  312.  
  313.  
  314.  
  315. end;
  316.  
  317. procedure TForm3.Button1Click(Sender: TObject);
  318. Var NumeroMouvement : String;
  319. Var XXcompte : String ;
  320. begin
  321.   /////////////validation des écritures
  322.  
  323.     /////////////verifier le DBComboBox1
  324.  
  325.     if DBLookupComboBox2.Text = '' then
  326.     begin
  327.     messagedlg('Le journal est VIDE , impossible de confirmer les écritures dans les lignes.',mtinformation,[mbok],0);
  328.     abort;
  329.     end;
  330.  
  331.     if Edit4.Text = '' then        ////////////// verifier la periode
  332.     begin
  333.     Edit4.Color:= clRed;
  334.     messagedlg('La periode est vide , selectionnez une periode dans la liste ou créez des periodes impossible de confirmer les écritures dans les lignes  .',mtinformation,[mbok],0);
  335.     abort;
  336.     end;
  337.  
  338.  
  339.  
  340.     if DBLookupComboBox1.Text <> '' then
  341.     begin
  342.      /////////////on verifie que le code du mouvement existe ou pas
  343.     if CodeMouvement.Text = '' then
  344.     begin
  345.     /////////Determine le code mouvement
  346.     Dbf4.Active:= True;
  347.     Dbf4.IndexName:= 'Mouvement';
  348.     Dbf4.Last;
  349.     NumeroMouvement := Dbf4.FieldByName('Mouvement').AsString;
  350.     end else begin
  351.           ////////LE code Mouvement existe , on le copy
  352.     NumeroMouvement := CodeMouvement.Text;
  353.     end;
  354.  
  355.  
  356.  
  357.       Tdbfcompte.IndexName := 'compte';
  358.       ////////boucle sur la table de saisie
  359.       dbf2.First;
  360.         while not dbf2.EOF do
  361.         begin
  362.         XXcompte := Dbf2.FieldByName('Mouvement').AsString;
  363.          if Tdbfcompte.SearchKeyPChar(PAnsiChar(AnsiString(XXcompte)), stGreaterEqual) then  //////////verification que le compte existe
  364.          begin
  365.          ///////// ajout dans les lignes apres que tout les comptes sont ok
  366.          end else begin
  367.               messagedlg('Lors de la verification le compte '+ XXcompte +' n est pas trouvé .',mtinformation,[mbok],0);
  368.               abort;
  369.          end;
  370.  
  371.  
  372.         end;
  373.  
  374.           /////// ajout dans la table lignes
  375.         dbf2.First;
  376.         while not dbf2.EOF do
  377.         begin
  378.         Dbf4.Append;
  379.         Dbf4.FieldByName('compte').AsString := Dbf2.FieldByName('compte').AsString;
  380.         Dbf4.FieldByName('label').AsString := Dbf2.FieldByName('label').AsString;
  381.         Dbf4.FieldByName('debit').AsString := Dbf2.FieldByName('debit').AsString;
  382.         Dbf4.FieldByName('credit').AsString := Dbf2.FieldByName('credit').AsString;
  383.         Dbf4.FieldByName('Mouvement').AsString := NumeroMouvement;
  384.         Dbf4.FieldByName('journal').AsString := NumeroMouvement;
  385.         Dbf4.FieldByName('periode').AsString := Edit4.Text;
  386.         Dbf4.FieldByName('createur').AsString := 'x';
  387.         Dbf4.FieldByName('dossier').AsString := 'x';
  388.         Dbf4.Post;
  389.         end;
  390.  
  391.  
  392.  
  393.  
  394.  
  395.     ////////affiche la form des lignes
  396.     if Form5.Visible = false then Form5.Visible := true else Form5.BringToFront ;
  397.  
  398.     end else begin
  399.     messagedlg('Le journal est VIDE , impossible de continuer .',mtinformation,[mbok],0);
  400.     end;
  401.  
  402. end;
  403.  
  404. procedure TForm3.Button2Click(Sender: TObject);
  405. var Xcompte : string;
  406. begin
  407.     //////////////////cherche un compte
  408.     Xcompte := Edit1.Text;
  409.     Tdbfcompte.IndexName:= 'compte';
  410.     if Tdbfcompte.Locate('compte', Xcompte, [loPartialKey, loCaseInsensitive]) then
  411.     begin
  412.     DBGrid2.SetFocus;
  413.     end;
  414.  
  415.  
  416. end;
  417.  
  418. procedure TForm3.Button3Click(Sender: TObject);
  419. begin
  420.   /////////////ajouter dans ligne de saisie le compte en cours  remplacement
  421.  
  422.     if DBEdit1.text <>'' then
  423.     begin
  424.        DBEdit1.Color := clGreen;
  425.  
  426.       DBGrid1.Columns[0].Field.Value := Tdbfcompte.FieldByName('compte').AsString;
  427.         if DBGrid1.Columns[1].Field.Value = '' Then
  428.         begin
  429.         DBGrid1.Columns[1].Field.Value := Tdbfcompte.FieldByName('label').AsString;
  430.         end;
  431.  
  432.  
  433.     end else begin
  434.           DBEdit1.Color := clRed ;
  435.           messagedlg('Compte NON Trouvé Ou Vide .',mtinformation,[mbok],0);
  436.           StatusBar1.SimpleText:= 'Ce compte n existe pas ';
  437.     end;
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444. end;
  445.  
  446. procedure TForm3.Button4Click(Sender: TObject);
  447. begin
  448.   //////////////////ajouter en fin de table
  449.         if DBEdit1.text <>'' then
  450.     begin
  451.     DBEdit1.Color := clGreen;
  452.     Dbf1.Append;
  453.     ////Dbf1.Edit;
  454.     Dbf1.FieldByName('compte').AsString := Tdbfcompte.FieldByName('compte').AsString;
  455.     Dbf1.FieldByName('label').AsString := Tdbfcompte.FieldByName('label').AsString;
  456.     Dbf1.Post;
  457.     end else begin
  458.           DBEdit1.Color := clRed ;
  459.           messagedlg('Compte NON Trouvé Ou Vide .',mtinformation,[mbok],0);
  460.           StatusBar1.SimpleText:= 'Ce compte n existe pas ';
  461.     end;
  462.  
  463. end;
  464.  
  465. procedure TForm3.Button5Click(Sender: TObject);
  466. begin
  467.   ////////////mettre le compte dans le champ de saisie individuel
  468.      try
  469.      Edit2.Text := Tdbfcompte.FieldByName('compte').AsString;
  470.      Edit3.Text := Tdbfcompte.FieldByName('label').AsString;
  471.      Edit3.SetFocus;
  472.      finally
  473.      end;
  474.  
  475.  
  476. end;
  477.  
  478. procedure TForm3.Button6Click(Sender: TObject);
  479. begin
  480.   /////////////////enlever les filtres des comptes
  481.     /////Tdbfcompte.cancelrange;
  482.     Tdbfcompte.Filtered := False;
  483.     Tdbfcompte.IndexName:= 'compte';
  484.     Tdbfcompte.First;
  485.     Tdbfcompte.Refresh;
  486.  
  487. end;
  488.  
  489. procedure TForm3.Button7Click(Sender: TObject);
  490. Var buttonSelected : Integer;
  491. begin
  492.   ///////////lignes de saisie à Zero
  493.   buttonSelected := MessageDlg('Supprimer les lignes de saisies ? ',mtCustom,
  494.                               [mbYes,mbCancel], 0);
  495.   if buttonSelected = mrYes    then
  496.   begin
  497.    Dbf1.Exclusive:= True;
  498.    Dbf1.Zap;
  499.    Dbf1.Exclusive:= False;
  500.    Dbf1.Refresh;
  501.    DBGrid1.Refresh;
  502.    calcullignes;
  503.    Tcredit.Text:='0';
  504.    Tdebit.Text:= '0';
  505.    Label2.Caption:= '';
  506.    Label2.Visible:= False;
  507.    Label7.Visible:= False;
  508.    ////messagedlg('Resultat Supprime ok  .',mtinformation,[mbok],0);
  509.   end;
  510.  
  511.   //////////
  512.  
  513. end;
  514.  
  515. procedure TForm3.Button8Click(Sender: TObject);
  516. begin
  517.   ////////////ajouter une ligne
  518.  
  519.  
  520.   Edit2.SetFocus;
  521.  
  522. calcullignes;
  523. end;
  524.  
  525. procedure TForm3.Button9Click(Sender: TObject);
  526. begin
  527. try
  528.   dbf1.Delete;
  529.   calcullignes;
  530. finally
  531. end;
  532. end;
  533.  
  534. procedure TForm3.DataSource1DataChange(Sender: TObject; Field: TField);
  535. begin
  536.   //////////Dbf1.Refresh;
  537. end;
  538.  
  539. procedure TForm3.DBComboBox2Change(Sender: TObject);
  540. begin
  541.  
  542. end;
  543.  
  544. procedure TForm3.Dbf1BeforeOpen(DataSet: TDataSet);
  545. begin
  546.  
  547. end;
  548.  
  549. procedure TForm3.calcullignes;
  550. Var TotalDebit : Extended;
  551. Var TotalCredit : Extended;
  552. Var SoldeDebit : Extended;
  553. Var SoldeCredit : Extended;
  554. Var NnnnLignes : Integer;
  555. begin
  556.  
  557.  
  558.         ////////////calculer les montant debit et credit
  559. NnnnLignes :=0;
  560.  
  561.  
  562.         Dbf2.DisableControls;
  563.         Dbf2.First;
  564.  
  565.         TotalDebit := 0;
  566.         TotalCredit :=0;
  567.          while not dbf2.EOF do
  568.         begin
  569.          NnnnLignes := NnnnLignes+1;
  570.           TotalDebit := TotalDebit + Dbf2.FieldByName('debit').AsFloat;
  571.           TotalCredit := TotalCredit + Dbf2.FieldByName('credit').AsFloat;
  572.           dbf2.Next;
  573.         end;
  574.  
  575.  
  576.          Dbf2.EnableControls;
  577.  
  578.         Tcredit.Text:= FloatToStr(TotalCredit);
  579.         Tdebit.Text:= FloatToStr(TotalDebit);
  580.  
  581.          if (TotalCredit = TotalDebit) AND ( (TotalDebit > 0) AND (TotalCredit > 0) )  then
  582.          begin
  583.          Tcredit.Color:=clLime;
  584.          Tdebit.Color:=clLime;
  585.          Label2.Caption := 'Validation Ok';
  586.          end else begin
  587.          Tcredit.Color:=clRed;
  588.          Tdebit.Color:=clRed;
  589.          Label2.Caption := 'Validation ImPossible';
  590.  
  591.          if TotalCredit > TotalDebit then
  592.          begin
  593.          SoldeDebit := TotalCredit - TotalDebit;
  594.           Label2.Caption := 'Solde Au Debit: ' + FloatToStr(SoldeDebit);
  595.          end else begin
  596.          SoldeCredit := TotalDebit - TotalCredit;
  597.           Label2.Caption := 'Solde Au Credit: ' + FloatToStr(SoldeCredit);
  598.          end;
  599.  
  600.  
  601.  
  602.  
  603.          end;
  604.  
  605.          Dbf2.Refresh;
  606.          Dbf1.Refresh;
  607.          DBGrid1.Refresh;
  608.          if (NnnnLignes>0) then
  609.          begin
  610.          Label7.Visible:= True;
  611.          Label7.Caption:= 'Nombre de Ligne : ' + IntToStr(NnnnLignes);
  612.  
  613.          end;
  614.  
  615. end;
  616.  
  617.  
  618.  
  619.  
  620.  
  621. end.
  622.  
« Last Edit: June 02, 2021, 05:31:04 pm by Fabius »

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: dbgrid and dbf
« Reply #6 on: June 02, 2021, 12:04:43 pm »
Code: Pascal  [Select][+][-]
  1.  
  2.  
  3. unit Unit3;
  4.  
  5. {$mode objfpc}{$H+}
  6.  
  7. interface
  8.  
  9. uses
  10.   Classes, SysUtils, DB, dbf, memds, Forms, Controls, Graphics, Dialogs,
  11.   StdCtrls, ComCtrls, DBGrids, Grids, DBCtrls, Spin, ExtCtrls, Buttons,
  12.   RTTIGrids, SpinEx;
  13.  
  14. type
  15.  
  16.   { TForm3 }
  17.  
  18.   TForm3 = class(TForm)
  19.     Bevel1: TBevel;
  20.     Bevel2: TBevel;
  21.     Bevel3: TBevel;
  22.     Button1: TButton;
  23.     Button2: TButton;
  24.     Button3: TButton;
  25.     Button4: TButton;
  26.     Button5: TButton;
  27.     Button6: TButton;
  28.     Button7: TButton;
  29.     Button8: TButton;
  30.     Button9: TButton;
  31.     ControlBar1: TControlBar;
  32.     DataSource1: TDataSource;
  33.     DataSource2: TDataSource;
  34.     DataSource3: TDataSource;
  35.     DataSource4: TDataSource;
  36.     DataSource5: TDataSource;
  37.     DataSource6: TDataSource;
  38.     DataSource7: TDataSource;
  39.     DBEdit1: TDBEdit;
  40.     DBEdit2: TDBEdit;
  41.     Dbf1: TDbf;
  42.     Dbf2: TDbf;
  43.     Dbf3: TDbf;
  44.     Dbf4: TDbf;
  45.     Dbf5: TDbf;
  46.     Dbf6: TDbf;
  47.     DBGrid2: TDBGrid;
  48.     DBGrid3: TDBGrid;
  49.     DBLookupComboBox1: TDBLookupComboBox;
  50.     DBLookupComboBox2: TDBLookupComboBox;
  51.     Edit1: TEdit;
  52.     Edit2: TEdit;
  53.     Edit3: TEdit;
  54.     CodeMouvement: TEdit;
  55.     Edit4: TEdit;
  56.     FloatSpinEditEx1: TFloatSpinEditEx;
  57.     FloatSpinEditEx2: TFloatSpinEditEx;
  58.     Label2: TLabel;
  59.     Label3: TLabel;
  60.     Label4: TLabel;
  61.     Label5: TLabel;
  62.     Label6: TLabel;
  63.     Label7: TLabel;
  64.     MemDataset1: TMemDataset;
  65.     Tdebit: TEdit;
  66.     Tcredit: TEdit;
  67.     Tdbfcompte: TDbf;
  68.     DBGrid1: TDBGrid;
  69.     Label1: TLabel;
  70.     StatusBar1: TStatusBar;
  71.     procedure Button1Click(Sender: TObject);
  72.     procedure Button2Click(Sender: TObject);
  73.     procedure Button3Click(Sender: TObject);
  74.     procedure Button4Click(Sender: TObject);
  75.     procedure Button5Click(Sender: TObject);
  76.     procedure Button6Click(Sender: TObject);
  77.     procedure Button7Click(Sender: TObject);
  78.     procedure Button8Click(Sender: TObject);
  79.     procedure Button9Click(Sender: TObject);
  80.     procedure DataSource1DataChange(Sender: TObject; Field: TField);
  81.     procedure DBComboBox2Change(Sender: TObject);
  82.     procedure Dbf1BeforeOpen(DataSet: TDataSet);
  83.    
  84.  
  85.     procedure DBGrid1ColExit(Sender: TObject);
  86.     procedure DBGrid1DblClick(Sender: TObject);
  87.     procedure DBGrid2Enter(Sender: TObject);
  88.     procedure DBGrid3DblClick(Sender: TObject);
  89.     procedure DBLookupComboBox1Change(Sender: TObject);
  90.     procedure Edit2Change(Sender: TObject);
  91.     procedure Edit2Exit(Sender: TObject);
  92.     procedure FloatSpinEditEx1Exit(Sender: TObject);
  93.     procedure FloatSpinEditEx2Change(Sender: TObject);
  94.     procedure FloatSpinEditEx2Exit(Sender: TObject);
  95.     procedure FormActivate(Sender: TObject);
  96.     procedure FormCreate(Sender: TObject);
  97.     procedure FormShow(Sender: TObject);
  98.  
  99.     procedure calcullignes;
  100.   private
  101.  
  102.   public
  103.  
  104.   end;
  105.  
  106. var
  107.   Form3: TForm3;
  108.  
  109. implementation
  110. uses Unit5 , Dbf_Common,dbf_dbffile,dbf_parser,dbf_prsdef,dbf_cursor,dbf_fields,dbf_pgfile,dbf_idxfile;
  111. {$R *.lfm}
  112.  
  113. { TForm3 }
  114.  
  115.  
  116.  
  117.  
  118. procedure TForm3.FormShow(Sender: TObject);
  119. begin
  120.   ////////
  121.     Form3.Top:= 100;
  122.     Form3.Left:= 100;
  123.           StatusBar1.SimpleText:= StatusBar1.Caption +' Calcul  ' ;
  124.  
  125.       Dbf1.Refresh;
  126.   DBGrid1.Refresh;
  127.  
  128.       calcullignes;
  129. end;
  130.  
  131. procedure TForm3.DBGrid1ColExit(Sender: TObject);
  132. var Xcompte : string;
  133. var Xlabel : string;
  134.  
  135. begin
  136.  
  137.      if (DBGrid1.SelectedField.FieldName = 'DEBIT') OR (DBGrid1.SelectedField.FieldName = 'CREDIT') then
  138.       begin
  139.       StatusBar1.SimpleText:= StatusBar1.Caption +' Calcul  ' ;
  140.       calcullignes;
  141.       end;
  142.  
  143.  
  144.  
  145.  
  146.     if DBGrid1.SelectedField.FieldName = 'COMPTE' then
  147.     begin
  148.       StatusBar1.SimpleText:= 'Recherche : ';
  149.       if (DBGrid1.Columns[0].Field.IsNull) then
  150.       begin
  151.  
  152.       end else begin
  153.       if (DBGrid1.Columns[0].Field.Value <> '') OR (DBGrid1.Columns[0].Field.Value <> 'Nil') then
  154.       begin
  155.       Xcompte := DBGrid1.Columns[0].Field.Value;
  156.       end else begin
  157.       Xcompte := '';
  158.       end;
  159.       end;
  160.  
  161.                          //////////recherche dans la table compte , si le compte saisie existe
  162.  
  163.       if (Xcompte <> '') then
  164.       begin
  165.  
  166.       Tdbfcompte.IndexName := 'compte';    ////table des comptes sur index compte
  167.       if Tdbfcompte.Locate('compte', Xcompte, [loPartialKey, loCaseInsensitive]) then
  168.       begin
  169.           ////////////liste les champs de la table sur l enregistrement
  170.           Button1.Enabled:= True ;
  171.           StatusBar1.SimpleText:= StatusBar1.Caption +' Trouve Ok ' ;
  172.  
  173.           Dbf1.Edit;
  174.           Dbf1.FieldByName('compte').AsString := Tdbfcompte.FieldByName('compte').AsString;
  175.           Dbf1.FieldByName('label').AsString := Tdbfcompte.FieldByName('label').AsString;
  176.           Dbf1.Post;
  177.  
  178.         Xlabel := Tdbfcompte.FieldByName('label').AsString;
  179.  
  180.         StatusBar1.SimpleText:= StatusBar1.Caption +' Ajouté ' + Xcompte + ' ' + Xlabel;
  181.         end else begin
  182.         Button1.Enabled:= False ;
  183.         StatusBar1.SimpleText := 'Non Trouvé : ' + Xcompte;
  184.         DBGrid1.SetFocus;
  185.         DBGrid1.SelectedColumn.FieldName := 'compte';
  186.         /////////Tdbfcompte. ['compte']. SetFocus;
  187.         end;
  188.     end;
  189.  
  190.      end else begin
  191.       StatusBar1.SimpleText := 'Error Vide : ';
  192.      end;
  193. end;
  194.  
  195. procedure TForm3.DBGrid1DblClick(Sender: TObject);
  196. begin
  197.   DBGrid1.Refresh;
  198. end;
  199.  
  200. procedure TForm3.DBGrid2Enter(Sender: TObject);
  201. Var Rcherche:String;
  202. Var Rliasse:String;
  203. begin
  204.   ///// le dbgrill des compte recois le focus on affiche la liasse auquel est attaché le compte
  205.      if Tdbfcompte.FieldByName('compte').AsString <> '' then
  206.      begin
  207.         Rcherche := Tdbfcompte.FieldByName('compte').AsString;
  208.         Tdbfcompte.IndexName := 'compte';
  209.         if Tdbfcompte.SearchKeyPChar(PAnsiChar(AnsiString(Rcherche)), stGreaterEqual) then
  210.         begin
  211.          Rliasse := Tdbfcompte.FieldByName('liasse').AsString;
  212.          /////pointeur sur le liasse correspondante
  213.          Dbf6.IndexName := 'liasse';
  214.          if Dbf6.SearchKeyPChar(PAnsiChar(AnsiString(Rliasse)), stGreaterEqual) then
  215.          begin
  216.          Dbf6.Refresh;
  217.          end;
  218.  
  219.         end;
  220.     end;
  221. end;
  222.  
  223. procedure TForm3.DBGrid3DblClick(Sender: TObject);
  224. Var FFF : String;
  225. begin
  226.   ////////////double clic sur liasse , on affiche les comptes associés
  227.  
  228.       if (DBEdit2.Text <> '') OR (DBEdit2.Text <> 'Nil') then
  229.       begin
  230.        //////////////appliquer filtre sur les comptes
  231.       FFF := DBEdit2.Text;
  232.       /////Tdbfcompte.IndexName:= 'liasse';
  233.       Tdbfcompte.Filter := 'liasse='+ QuotedStr(FFF);
  234.       Tdbfcompte.Filtered := true;
  235.       end;
  236.  
  237. //   Tdbfcompte
  238. end;
  239.  
  240. procedure TForm3.DBLookupComboBox1Change(Sender: TObject);
  241. begin
  242.   Edit4.Text:= DBLookupComboBox1.Text;
  243.   if Dbf1.RecordCount = 0 then
  244.   begin
  245.   Edit4.Color:=clblue;
  246.   end else begin
  247.    Edit4.Color:=clGreen;
  248.   end;
  249. end;
  250.  
  251. procedure TForm3.Edit2Change(Sender: TObject);
  252. Var Rrrrr : String;
  253. begin
  254.   ////////////////champ de saissie  compte , il change on change la liste des comptes
  255.     Rrrrr := Edit2.Text;
  256.     Tdbfcompte.IndexName:= 'compte';
  257.     if Tdbfcompte.Locate('compte', Rrrrr, [loPartialKey, loCaseInsensitive]) then
  258.     begin
  259.     Tdbfcompte.Refresh;
  260.     Edit3.Text := Tdbfcompte.FieldByName('label').AsString;
  261.     end;
  262.  
  263. end;
  264.  
  265. procedure TForm3.Edit2Exit(Sender: TObject);
  266. begin
  267.    Edit3.SetFocus;
  268. end;
  269.  
  270. procedure TForm3.FloatSpinEditEx1Exit(Sender: TObject);
  271. begin
  272.    Button8.SetFocus;
  273.  
  274. end;
  275.  
  276. procedure TForm3.FloatSpinEditEx2Change(Sender: TObject);
  277. begin
  278.  
  279. end;
  280.  
  281. procedure TForm3.FloatSpinEditEx2Exit(Sender: TObject);
  282. begin
  283.   FloatSpinEditEx1.SetFocus;
  284. end;
  285.  
  286. procedure TForm3.FormActivate(Sender: TObject);
  287. begin
  288.   Dbf1.Refresh;
  289.   DBGrid1.Refresh;
  290.   DBGrid1.SetFocus;
  291.   DBGrid1.Update;
  292.   calcullignes;
  293. end;
  294.  
  295. procedure TForm3.FormCreate(Sender: TObject);
  296. begin
  297.   ////// les chemins pour les tables
  298.  
  299.   ///       CodeMouvement.Color:=;
  300.  
  301.  
  302.   Dbf1.First;
  303.   if Dbf1.RecordCount = 0 then
  304.   begin
  305.        Edit4.Color:=clblue;
  306.   end else begin
  307.      if Edit4.Text <> '' then
  308.      begin
  309.        Edit4.Color:=clGreen;
  310.        end else begin
  311.        Edit4.Color:=clblue;
  312.      end;
  313.   end;
  314.  
  315.  
  316.  
  317. end;
  318.  
  319. procedure TForm3.Button1Click(Sender: TObject);
  320. Var NumeroMouvement : String;
  321. Var XXcompte : String ;
  322. begin
  323.   /////////////validation des écritures
  324.  
  325.     /////////////verifier le DBComboBox1
  326.  
  327.     if DBLookupComboBox2.Text = '' then
  328.     begin
  329.     messagedlg('Le journal est VIDE , impossible de confirmer les écritures dans les lignes.',mtinformation,[mbok],0);
  330.     abort;
  331.     end;
  332.  
  333.     if Edit4.Text = '' then        ////////////// verifier la periode
  334.     begin
  335.     Edit4.Color:= clRed;
  336.     messagedlg('La periode est vide , selectionnez une periode dans la liste ou créez des periodes impossible de confirmer les écritures dans les lignes  .',mtinformation,[mbok],0);
  337.     abort;
  338.     end;
  339.  
  340.  
  341.  
  342.     if DBLookupComboBox1.Text <> '' then
  343.     begin
  344.      /////////////on verifie que le code du mouvement existe ou pas
  345.     if CodeMouvement.Text = '' then
  346.     begin
  347.     /////////Determine le code mouvement
  348.     Dbf4.Active:= True;
  349.     Dbf4.IndexName:= 'Mouvement';
  350.     Dbf4.Last;
  351.     NumeroMouvement := Dbf4.FieldByName('Mouvement').AsString;
  352.     end else begin
  353.           ////////LE code Mouvement existe , on le copy
  354.     NumeroMouvement := CodeMouvement.Text;
  355.     end;
  356.  
  357.  
  358.  
  359.       Tdbfcompte.IndexName := 'compte';
  360.       ////////boucle sur la table de saisie
  361.       dbf2.First;
  362.         while not dbf2.EOF do
  363.         begin
  364.         XXcompte := Dbf2.FieldByName('Mouvement').AsString;
  365.          if Tdbfcompte.SearchKeyPChar(PAnsiChar(AnsiString(XXcompte)), stGreaterEqual) then  //////////verification que le compte existe
  366.          begin
  367.          ///////// ajout dans les lignes apres que tout les comptes sont ok
  368.          end else begin
  369.               messagedlg('Lors de la verification le compte '+ XXcompte +' n est pas trouvé .',mtinformation,[mbok],0);
  370.               abort;
  371.          end;
  372.  
  373.  
  374.         end;
  375.  
  376.           /////// ajout dans la table lignes
  377.         dbf2.First;
  378.         while not dbf2.EOF do
  379.         begin
  380.         Dbf4.Append;
  381.         Dbf4.FieldByName('compte').AsString := Dbf2.FieldByName('compte').AsString;
  382.         Dbf4.FieldByName('label').AsString := Dbf2.FieldByName('label').AsString;
  383.         Dbf4.FieldByName('debit').AsString := Dbf2.FieldByName('debit').AsString;
  384.         Dbf4.FieldByName('credit').AsString := Dbf2.FieldByName('credit').AsString;
  385.         Dbf4.FieldByName('Mouvement').AsString := NumeroMouvement;
  386.         Dbf4.FieldByName('journal').AsString := NumeroMouvement;
  387.         Dbf4.FieldByName('periode').AsString := Edit4.Text;
  388.         Dbf4.FieldByName('createur').AsString := 'x';
  389.         Dbf4.FieldByName('dossier').AsString := 'x';
  390.         Dbf4.Post;
  391.         end;
  392.  
  393.  
  394.  
  395.  
  396.  
  397.     ////////affiche la form des lignes
  398.     if Form5.Visible = false then Form5.Visible := true else Form5.BringToFront ;
  399.  
  400.     end else begin
  401.     messagedlg('Le journal est VIDE , impossible de continuer .',mtinformation,[mbok],0);
  402.     end;
  403.  
  404. end;
  405.  
  406. procedure TForm3.Button2Click(Sender: TObject);
  407. var Xcompte : string;
  408. begin
  409.     //////////////////cherche un compte
  410.     Xcompte := Edit1.Text;
  411.     Tdbfcompte.IndexName:= 'compte';
  412.     if Tdbfcompte.Locate('compte', Xcompte, [loPartialKey, loCaseInsensitive]) then
  413.     begin
  414.     DBGrid2.SetFocus;
  415.     end;
  416.  
  417.  
  418. end;
  419.  
  420. procedure TForm3.Button3Click(Sender: TObject);
  421. begin
  422.   /////////////ajouter dans ligne de saisie le compte en cours  remplacement
  423.  
  424.     if DBEdit1.text <>'' then
  425.     begin
  426.        DBEdit1.Color := clGreen;
  427.  
  428.       DBGrid1.Columns[0].Field.Value := Tdbfcompte.FieldByName('compte').AsString;
  429.         if DBGrid1.Columns[1].Field.Value = '' Then
  430.         begin
  431.         DBGrid1.Columns[1].Field.Value := Tdbfcompte.FieldByName('label').AsString;
  432.         end;
  433.  
  434.  
  435.     end else begin
  436.           DBEdit1.Color := clRed ;
  437.           messagedlg('Compte NON Trouvé Ou Vide .',mtinformation,[mbok],0);
  438.           StatusBar1.SimpleText:= 'Ce compte n existe pas ';
  439.     end;
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446. end;
  447.  
  448. procedure TForm3.Button4Click(Sender: TObject);
  449. begin
  450.   //////////////////ajouter en fin de table
  451.         if DBEdit1.text <>'' then
  452.     begin
  453.     DBEdit1.Color := clGreen;
  454.     Dbf1.Append;
  455.     ////Dbf1.Edit;
  456.     Dbf1.FieldByName('compte').AsString := Tdbfcompte.FieldByName('compte').AsString;
  457.     Dbf1.FieldByName('label').AsString := Tdbfcompte.FieldByName('label').AsString;
  458.     Dbf1.Post;
  459.     end else begin
  460.           DBEdit1.Color := clRed ;
  461.           messagedlg('Compte NON Trouvé Ou Vide .',mtinformation,[mbok],0);
  462.           StatusBar1.SimpleText:= 'Ce compte n existe pas ';
  463.     end;
  464.  
  465. end;
  466.  
  467. procedure TForm3.Button5Click(Sender: TObject);
  468. begin
  469.   ////////////mettre le compte dans le champ de saisie individuel
  470.      try
  471.      Edit2.Text := Tdbfcompte.FieldByName('compte').AsString;
  472.      Edit3.Text := Tdbfcompte.FieldByName('label').AsString;
  473.      Edit3.SetFocus;
  474.      finally
  475.      end;
  476.  
  477.  
  478. end;
  479.  
  480. procedure TForm3.Button6Click(Sender: TObject);
  481. begin
  482.   /////////////////enlever les filtres des comptes
  483.     /////Tdbfcompte.cancelrange;
  484.     Tdbfcompte.Filtered := False;
  485.     Tdbfcompte.IndexName:= 'compte';
  486.     Tdbfcompte.First;
  487.     Tdbfcompte.Refresh;
  488.  
  489. end;
  490.  
  491. procedure TForm3.Button7Click(Sender: TObject);
  492. Var buttonSelected : Integer;
  493. begin
  494.   ///////////lignes de saisie à Zero
  495.   buttonSelected := MessageDlg('Supprimer les lignes de saisies ? ',mtCustom,
  496.                               [mbYes,mbCancel], 0);
  497.   if buttonSelected = mrYes    then
  498.   begin
  499.    Dbf1.Exclusive:= True;
  500.    Dbf1.Zap;
  501.    Dbf1.Exclusive:= False;
  502.    Dbf1.Refresh;
  503.    DBGrid1.Refresh;
  504.    calcullignes;
  505.    Tcredit.Text:='0';
  506.    Tdebit.Text:= '0';
  507.    Label2.Caption:= '';
  508.    Label2.Visible:= False;
  509.    Label7.Visible:= False;
  510.    ////messagedlg('Resultat Supprime ok  .',mtinformation,[mbok],0);
  511.   end;
  512.  
  513.   //////////
  514.  
  515. end;
  516.  
  517. procedure TForm3.Button8Click(Sender: TObject);
  518. begin
  519.   ////////////ajouter une ligne
  520.  
  521.  
  522.   Edit2.SetFocus;
  523.  
  524. calcullignes;
  525. end;
  526.  
  527. procedure TForm3.Button9Click(Sender: TObject);
  528. begin
  529. try
  530.   dbf1.Delete;
  531.   calcullignes;
  532. finally
  533. end;
  534. end;
  535.  
  536. procedure TForm3.DataSource1DataChange(Sender: TObject; Field: TField);
  537. begin
  538.   //////////Dbf1.Refresh;
  539. end;
  540.  
  541. procedure TForm3.DBComboBox2Change(Sender: TObject);
  542. begin
  543.  
  544. end;
  545.  
  546. procedure TForm3.Dbf1BeforeOpen(DataSet: TDataSet);
  547. begin
  548.  
  549. end;
  550.  
  551. procedure TForm3.calcullignes;
  552. Var TotalDebit : Extended;
  553. Var TotalCredit : Extended;
  554. Var SoldeDebit : Extended;
  555. Var SoldeCredit : Extended;
  556. Var NnnnLignes : Integer;
  557. begin
  558.  
  559.  
  560.         ////////////calculer les montant debit et credit
  561. NnnnLignes :=0;
  562.  
  563.  
  564.         Dbf2.DisableControls;
  565.         Dbf2.First;
  566.  
  567.         TotalDebit := 0;
  568.         TotalCredit :=0;
  569.          while not dbf2.EOF do
  570.         begin
  571.          NnnnLignes := NnnnLignes+1;
  572.           TotalDebit := TotalDebit + Dbf2.FieldByName('debit').AsFloat;
  573.           TotalCredit := TotalCredit + Dbf2.FieldByName('credit').AsFloat;
  574.           dbf2.Next;
  575.         end;
  576.  
  577.  
  578.          Dbf2.EnableControls;
  579.  
  580.         Tcredit.Text:= FloatToStr(TotalCredit);
  581.         Tdebit.Text:= FloatToStr(TotalDebit);
  582.  
  583.          if (TotalCredit = TotalDebit) AND ( (TotalDebit > 0) AND (TotalCredit > 0) )  then
  584.          begin
  585.          Tcredit.Color:=clLime;
  586.          Tdebit.Color:=clLime;
  587.          Label2.Caption := 'Validation Ok';
  588.          end else begin
  589.          Tcredit.Color:=clRed;
  590.          Tdebit.Color:=clRed;
  591.          Label2.Caption := 'Validation ImPossible';
  592.  
  593.          if TotalCredit > TotalDebit then
  594.          begin
  595.          SoldeDebit := TotalCredit - TotalDebit;
  596.           Label2.Caption := 'Solde Au Debit: ' + FloatToStr(SoldeDebit);
  597.          end else begin
  598.          SoldeCredit := TotalDebit - TotalCredit;
  599.           Label2.Caption := 'Solde Au Credit: ' + FloatToStr(SoldeCredit);
  600.          end;
  601.  
  602.  
  603.  
  604.  
  605.          end;
  606.  
  607.          Dbf2.Refresh;
  608.          Dbf1.Refresh;
  609.          DBGrid1.Refresh;
  610.          if (NnnnLignes>0) then
  611.          begin
  612.          Label7.Visible:= True;
  613.          Label7.Caption:= 'Nombre de Ligne : ' + IntToStr(NnnnLignes);
  614.  
  615.          end;
  616.  
  617. end;
  618.  
  619.  
  620.  
  621.  
  622.  
  623. end.
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  

Please use the "#" above in the menus so you can paste your code between and it will show like you see here, now.

Please package your program within a ZIP file and attached it here.

Use the menus provided in Lazarus.

PROJECT:
  PUBLISH PROJECT

it maybe different in your language.

« Last Edit: June 02, 2021, 12:13:45 pm by jamie »
The only true wisdom is knowing you know nothing

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: dbgrid and dbf
« Reply #7 on: June 02, 2021, 12:21:10 pm »
I don't look at source-code where units, variables, controls etc. haven't been properly named....

I'm not going to guess, what Button1 stands for......
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: dbgrid and dbf
« Reply #8 on: June 02, 2021, 12:42:06 pm »
I can understand much of what's there. 45 years of coding helps with that grey matter functionality up there. :o

I am sure if you were looking at the code with samples it would come to you quickly..

The only true wisdom is knowing you know nothing

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: dbgrid and dbf
« Reply #9 on: June 02, 2021, 02:38:54 pm »
Jamie,
yeah, i know.
Call me a lazy bugger  :D :D :D :P :P :P :P
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Fabius

  • Jr. Member
  • **
  • Posts: 59
Re: dbgrid and dbf
« Reply #10 on: June 05, 2021, 11:20:58 am »
I found, the problem comes from not putting an index in the dbgrid  :o

 

TinyPortal © 2005-2018