Bonjour, ca fait un moment que je planche dessus mais je n'arrive à trouver comment faire... Lorsque je veux trier une colonne en alphanumérique, j'obtiens les chiffres dans cet ordre : 1, 10, 100, 2, 23456789, 3, ... ect
Je vous mets le code de ma fiche en dessous, si quelqu'un est assez courageux pour me dire ce qu'il faudrait changer/ajouter... :s

La fonction de départ est Tri()

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
unit Trier;
 
interface
 
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Typess,
  DBTables,DB, StdCtrls,Dialogs,variants,FonctionsMBDS;
 
type
  TTrierForm = class(TForm)
    MemoAide: TMemo;
    Fermer: TButton;
    OpenDialogDetruireTable: TOpenDialog;
    TableSupprime: TTable;
    BatchMoveSqlVersTable: TBatchMove;
    BatchMoveTableVersTable: TBatchMove;
    procedure FormShow(Sender: TObject);
  private
    { Déclarations privées }
    MaxEnreg : integer;
    stringAuSensVrai,BaseOccupee : boolean;
    TrieAvecSQL : integer;
    ListEnregOrigine : Tstringlist; // contient les enregistrements de la colonne que l'on souhaite triée
    procedure RechercheFauxString;
    procedure PassageTrieEnSql;
    function  RechercheApplicationAlias : boolean;
  public
    { Déclarations publiques }
    ListChamp,ListType,ListIndex,ListTaille : TstringList;
    // ...Lck,TableTemp : Boolean;
    CheminFichier,NomTableTemporaire,NomTableApresTrie: String;
    Procedure EnregistrerTable(NumeroFicheTri : integer);
    function  ConcatenationDroiteGauche(nbc : integer; chaine : string): string;
    procedure TableTempoTrie();
    Function  ListeDesIndexTableTempo : string;
    Procedure AjouterIndex(NomColonne : string);
    Procedure Tri();
    Procedure UneApplicationLance();
    Procedure PlusieursApplicationLance();
    Procedure DetruireIndexSecondaire(NumeroFicheTri : integer);
    procedure RemplirListes();
    procedure SiModificationTableTempo(DataSet: TDataSet);
  end;
 
var
  TrierForm: TTrierForm;
 
implementation
 
uses Main, fichiers, VisualiseTable, CommunicationInterQuaiRecp;
 
{$R *.DFM}
Procedure TTrierForm.EnregistrerTable(NumeroFicheTri : integer);
var k,j,differenceCaract  : integer;
    Intermediaire,EnregEnCour,MaTable : string;
begin
  try
     MainForm.NomColonne := MainForm.TableauGestionColonnesMisAJour[NumeroFicheTri];
     if Mainform.ActiveMDIChild.Tag > 0 then
     begin
        MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].MessageDePatience(false,'');
        MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].DBgridPlusieursTables.visible := False;
        MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].StringGridExtended1.visible := False;    //28/10/2009
        MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].StringGridExtended1.StringGrid1.visible := False; 
     end;
     MaTable := MainForm.MultiTableForm[NumeroFicheTri].UneTable.TableName;
     if pos ('.', MaTable) <> 0 then
        //delete(MaTable,(length(MaTable)-2),3);
     if Mainform.ActiveMDIChild.Tag > 0 then
     begin
         if MainForm.TableauGestionDonneesModifieeslocal[NumeroFicheTri] then
         MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].LeMessage.Caption := 'Des données ont été modifiées par le programme lors du tri.'+#13#10+'Veuillez patienter: réinitialisation des enregistrements de la table '+MaTable
         else MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].LeMessage.Caption := 'Veuillez patienter: sauvegarde des données modifiées lors du tri';
         MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].LeMessage.refresh;
         MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].Gauge.BackColor := clWhite;
         MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].Gauge.ForeColor := clBlue;
         MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].Gauge.Visible := True;
         MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].Gauge.Progress  := 10;
     end;
     if MainForm.TableTemp = true then
     with MainForm.TableTempoTri[NumeroFicheTri] do
     begin
          Close;
          MainForm.MultiTableForm[NumeroFicheTri].UneTable.Close;
          if MainForm.Base.CheminAlias <> '' then
          begin
               Databasename := MainForm.Base.CheminAlias;
               MainForm.MultiTableForm[NumeroFicheTri].UneTable.DatabaseName := MainForm.Base.CheminAlias;
          end
          else
          begin
               Databasename := MainForm.Base.CheminParDefaut;
               MainForm.MultiTableForm[NumeroFicheTri].UneTable.DatabaseName := MainForm.Base.CheminParDefaut;
          end;
          TableName:= 'TableTempoTrie'+MainForm.Base.NomBase;//Name recupère le nom de la table
          MainForm.MultiTableForm[NumeroFicheTri].UneTable.TableName := MainForm.NomTable;
          MainForm.MultiTableForm[NumeroFicheTri].UneTable.SessionName := MainForm.ModBasDonSession.SessionName;
          MainForm.MultiTableForm[NumeroFicheTri].UneTable.TableName := MaTable;
          MainForm.MultiTableForm[NumeroFicheTri].UneTable.ReadOnly := False;
          if TrieAvecSQL <> 1 then
             MainForm.TableTempoTri[NumeroFicheTri].IndexName := '';
          if Mainform.ActiveMDIChild.Tag >= 1 then
             MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].Gauge.Progress := 50;
          if  (Mainform.TriParTableTempoAvecModif) and (not MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].HorsAlias) then   //30/11/2006 AS
          begin
                BatchMoveTableVersTable.Source := MainForm.TableTempoTri[NumeroFicheTri];
                BatchMoveTableVersTable.Destination := MainForm.MultiTableForm[NumeroFicheTri].UneTable;
                BatchMoveTableVersTable.AbortOnKeyViol := False;
                if MainForm.TableNonIndexee then
                   BatchMoveTableVersTable.Mode := batcopy
                else
                   BatchMoveTableVersTable.Mode := batAppendUpdate;
                Screen.Cursor := crAppStart;
                try
                  BatchMoveTableVersTable.Execute;
                  MainForm.TableauGestionDonneesModifieeslocal[Mainform.ActiveMDIChild.Tag -1] := false;
                except on exception do
                begin
                   Screen.Cursor := crDefault;
                   MsgDlgMultiEcran('Le tri a échoué !',mtError,[mbok],0);
                   exit;
                end;
                end;
                Screen.Cursor := crDefault;
          end;
          if Mainform.ActiveMDIChild.Tag >= 1 then
             MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].Gauge.Progress := 100;
          MainForm.TableTempoTri[NumeroFicheTri].Close;
          if MainForm.TableTempoTri[NumeroFicheTri].Exists then
          begin
             MainForm.TableTempoTri[NumeroFicheTri].DeleteTable;
             MainForm.TableTempoTri[NumeroFicheTri] := nil;
             Mainform.TriParTableTempoAvecModif := false;
             Mainform.PasDeModification := false;
          end;
          MainForm.TableTemp := False;
          MainForm.MultiTableForm[NumeroFicheTri].DBgridPlusieursTables.visible := True;
          MainForm.MultiTableForm[NumeroFicheTri].DatasourcePlusieursTables.DataSet := MainForm.MultiTableForm[NumeroFicheTri].UneTable;
          MainForm.MultiTableForm[NumeroFicheTri].DatasourcePlusieursTables.Enabled := true;
          MainForm.MultiTableForm[NumeroFicheTri].DBgridPlusieursTables.DataSource := MainForm.MultiTableForm[NumeroFicheTri].DatasourcePlusieursTables;
 
          try
            MainForm.MultiTableForm[NumeroFicheTri].UneTable.active := true;
          except on exception do   //30/11/2006 Si trie Hors Alias
          begin
              if (MainForm.MultiTableForm[NumeroFicheTri].Trie_HorsAlias) or (pos('TableTempoTrie',MainForm.MultiTableForm[NumeroFicheTri].caption) <> 0) then
                 if MainForm.MultiTableForm[NumeroFicheTri].Chemin_HorsAlias <> '' then
                    MainForm.MultiTableForm[NumeroFicheTri].UneTable.databasename := MainForm.MultiTableForm[NumeroFicheTri].Chemin_HorsAlias ;
                    try
                      MainForm.MultiTableForm[NumeroFicheTri].UneTable.active := true;
                    except on exception do end;
          end;
          end;
     end
     else
     if MainForm.NomColonne <> '' then
       if MainForm.TableauGestionDonneesModifieeslocal[NumeroFicheTri] then
       begin
            if TrieAvecSQL <> 1 then
               MainForm.MultiTableForm[NumeroFicheTri].UneTable.IndexName := '';
            MainForm.MultiTableForm[NumeroFicheTri].UneTable.Close;
            MainForm.MultiTableForm[NumeroFicheTri].UneTable.ReadOnly := False;
            MainForm.MultiTableForm[NumeroFicheTri].UneTable.Open;
            k := 0;
            MainForm.Refresh;
            if Mainform.ActiveMDIChild.Tag -1 > 0 then
               MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].Gauge.Progress := 20;
            MainForm.MultiTableForm[NumeroFicheTri].UneTable.Refresh;
            MainForm.MultiTableForm[NumeroFicheTri].UneTable.active := false;
            MainForm.MultiTableForm[NumeroFicheTri].UneTable.active := true;
            MainForm.MultiTableForm[NumeroFicheTri].UneTable.First;
            MainForm.MultiTableForm[NumeroFicheTri].UneTable.IndexName := '';
            While not MainForm.MultiTableForm[NumeroFicheTri].UneTable.Eof do
            begin
                            differenceCaract := MaxEnreg - Length(ListEnregOrigine[k]);
                            If MainForm.MultiTableForm[NumeroFicheTri].UneTable.FieldByname(MainForm.NomColonne).Value = null then EnregEnCour := ''
                            else
                            EnregEnCour := MainForm.MultiTableForm[NumeroFicheTri].UneTable.FieldByname(MainForm.NomColonne).Value;
                            if differenceCaract > 0 then
                            begin
                               for j := 0 to differenceCaract -1 do
                                   if pos('0',EnregEnCour) <> 0 then
                                   begin
                                        Intermediaire := EnregEnCour;
                                        if copy(Intermediaire,1,1) = '0' then
                                         delete(EnregEnCour,1,1);
                                   end;
                               MainForm.MultiTableForm[NumeroFicheTri].UneTable.edit;
                               if  EnregEnCour = '' then MainForm.MultiTableForm[NumeroFicheTri].UneTable.FieldByname(MainForm.NomColonne).Value := null
                               else
                                  MainForm.MultiTableForm[NumeroFicheTri].UneTable.FieldByname(MainForm.NomColonne).Value := EnregEnCour;
                            end;
                            MainForm.MultiTableForm[NumeroFicheTri].UneTable.Next;
                  inc(k);
                  if Mainform.ActiveMDIChild.Tag > 0 then
                     MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].Gauge.Progress  := 20+ (round(((80*k) / MainForm.NbreEnreg )));
             end;
       end;
     if (TrieAvecSQL <> 1) and (MainForm.TableTempoTri[NumeroFicheTri] <> nil) then
        MainForm.TableTempoTri[NumeroFicheTri].IndexName := '';
     if TrieAvecSQL <> 1 then
        MainForm.MultiTableForm[NumeroFicheTri].UneTable.IndexName := '';
     MainForm.MultiTableForm[NumeroFicheTri].UneTable.Close;
     if Mainform.ActiveMDIChild.Tag > 0 then
     begin
         MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].MessageDePatience(true,'');
         MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].Gauge.Visible := False;
         MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].DBgridPlusieursTables.visible := True;
     end;
     MainForm.NomColonne := '';
     MainForm.TableauGestionDonneesModifieeslocal[NumeroFicheTri] := false;
     MainForm.TableauGestionColonnesMisAJour[NumeroFicheTri] := '';
     MainForm.Edition := False;
     MainForm.TableFermer := False;
     MainForm.ToolButton_Editer.Down := False;
     ListChamp.free;
     if ListEnregOrigine <> nil then
     begin
        ListEnregOrigine.Free;
        ListEnregOrigine := nil;
     end;
     if MainForm.ActiveMDIChild.Tag > 0 then
        DetruireIndexSecondaire(NumeroFicheTri);
     MainForm.MultiTableForm[NumeroFicheTri].UneTable.TableName := MaTable;
     MainForm.MultiTableForm[NumeroFicheTri].UneTable.active := true;
     if MainForm.ActiveMDIChild.Tag > 0 then
     begin
           MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DBgridPlusieursTables.visible := True;
           MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DatasourcePlusieursTables.DataSet := MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable;
           MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DatasourcePlusieursTables.Enabled := true;
           MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DBgridPlusieursTables.DataSource := MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DatasourcePlusieursTables;
           MainForm.MultiTableForm[NumeroFicheTri].UneTable.active := true;
           MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].MessageDePatience(true,'');
     end;
     MainForm.MultiTableForm[NumeroFicheTri].UneTable.active := false;
     MainForm.MultiTableForm[NumeroFicheTri].OnActivate(nil);
  except on exception do end;
end;
 
Procedure TTrierForm.DetruireIndexSecondaire(NumeroFicheTri : integer);
var MaTable,ComposantX,ComposantY : string;
    i : integer;
begin
       MaTable := MainForm.MultiTableForm[NumeroFicheTri].UneTable.TableName;
       if MainForm.MultiTableForm[NumeroFicheTri].UneTable.Exists then
          delete(MaTable,(length(MaTable)-2),3);
       for i := 0 to 20  do
       begin
            ComposantX := MainForm.Base.CheminAlias+'/'+MaTable+'.XG'+inttostr(i);
            ComposantY := MainForm.Base.CheminAlias+'/'+MaTable+'.YG'+inttostr(i);
            If  FileExists(ComposantX) then
            begin
                 DeleteFile(ComposantX);
                 DeleteFile(ComposantY);
            end 
            else break;
       end;
end;
 
 
{ =====================================================================
  Fonction   : ConcatenationDroiteGauche
  But        : Concaténe des blanc à droite d'une chaine de caractères
  Paramètres : sens : sens de la concatenation : False concaténation à droite
                                                 True                à gauche
               nbc  : nombre de caractères de la chaine
               Chaine : chaine à concaténée
  Retour     :
  =====================================================================}
function TTrierForm.ConcatenationDroiteGauche(nbc : integer; chaine : string): string;
var
i, LongChaine, lg : integer;
retour : string;
begin
  //*MainForm.DonneesModifiees := True;
  MainForm.TableauGestionDonneesModifieeslocal[Mainform.ActiveMDIChild.Tag -1] := true;
  MainForm.TableauGestionColonnesMisAJour[Mainform.ActiveMDIChild.Tag -1] := MainForm.NomColonne;
  if Chaine =  '' then ConcatenationDroiteGauche := ''
  else
  begin
       LongChaine := Length(chaine);
       lg := nbc - LongChaine;
       retour := '';
       for i:=0 to lg-1 do
       begin
            //retour := retour + ' ';
            retour := retour + '0';
       end;
       retour := retour + chaine;
       ConcatenationDroiteGauche := retour;
  end;
end;
 
// Nouvelle Modification du 25/07/02
 
 
//Création d'une table temporaire fonctionnant le temps de trier la table d'origine et detruite par la suite.
procedure TTrierForm.TableTempoTrie();
var
    i,k,taille:integer;
    TousChamps : string;
begin
    MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DBgridPlusieursTables.Visible := False;
    MainForm.MultiTableForm[Mainform.ActiveMDIChild.Tag -1].StringGridExtended1.visible := False;   //28/10/2009
    MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DatasourcePlusieursTables.Enabled := False;
    // TableTemp est vrai uniquement quand plusieurs applications sont lancés
    MainForm.TableTemp := True;
    ListChamp := TstringList.Create();
    ListType  := TstringList.Create();
    ListIndex := TstringList.Create();
    ListTaille := TstringList.Create();
    ListChamp.Clear;
    ListType.Clear;
    ListIndex.Clear;
    ListTaille.Clear;
    with MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable do
    begin
       Active := True;
       For i := 0 to FieldCount -1 do
            begin
                 ListChamp.Add(Fields[i].FieldName);
                 case  Fields[i].Datatype of
                       ftString	:
                       begin
                            ListType.Add('A');
                            Taille:= Fields[i].Size;
                            ListTaille.Add(IntToStr(Taille));
                       end;
                       ftSmallint	:
                       begin
                            ListType.Add('S');
                            ListTaille.Add(IntToStr(0));
                       end;
                       ftInteger	:
                       begin
                            ListType.Add('I');
                            ListTaille.Add(IntToStr(0));
                       end;
                       ftDate	:
                       begin
                            ListType.Add('D');
                            ListTaille.Add(IntToStr(0));
                       end;
                       ftTime	:
                       begin
                            ListType.Add('T');
                            ListTaille.Add(IntToStr(0));
                       end;
                       ftDateTime	:
                       begin
                            ListType.Add('@');
                            ListTaille.Add(IntToStr(0));
                       end;
                       ftCurrency	:
                       begin
                            ListType.Add('$');
                            ListTaille.Add(IntToStr(0));
                       end;
                       ftFloat	:
                       begin
                            ListType.Add('N');
                            ListTaille.Add(IntToStr(0));
                       end;
                       ftBoolean   :
                       begin
                            ListType.Add('L');
                            ListTaille.Add(IntToStr(0));
                       end;
                       ftBytes	:
                       begin
                            ListType.Add('Y');
                            ListTaille.Add(IntToStr(0));
                       end;
                       ftVarBytes  :
                       begin
                            ListType.Add('B');
                            ListTaille.Add(IntToStr(0));
                       end;
                        ftAutoInc  :
                       begin
                            ListType.Add('+');
                            ListTaille.Add(IntToStr(0));
                       end;
                       else
                       begin
                         ListType.Add('');
                         ListTaille.Add(IntToStr(0));
                       end;
                 end;
            end;
       for I := 0 to IndexFieldCount - 1 do
           //ListIndex.add(TableOuverture.IndexFields[I].FieldName);
           ListIndex.add(Fields[I].FieldName);
    end;
    k := 0;
    MainForm.TableTempoTri[MainForm.ActiveMDIChild.Tag -1] := TTable.Create(self);
    MainForm.TableTempoTri[MainForm.ActiveMDIChild.Tag -1].AfterPost := SiModificationTableTempo;
    with MainForm.TableTempoTri[MainForm.ActiveMDIChild.Tag -1] do
    begin
         Close;
         TableType := ttParadox;
         if MainForm.Base.CheminAlias <> '' then
            Databasename := MainForm.Base.CheminAlias
         else
            Databasename := MainForm.Base.CheminParDefaut;
         TableName:= 'TableTempoTrie'+MainForm.Base.NomBase;//Name recupère le nom de la table
         NomTableTemporaire :=  TableName;
         SessionName := MainForm.ModBasDonSession.SessionName;
         with  FieldDefs do // Description des champs de la table
         begin
              Clear;
              for i:= 0 to  ListChamp.Count - 1 do
              begin
                   if ListType[i] = 'A'  then Add(ListChamp[i], ftstring,StrToIntDef(ListTaille[i],25),False);
                   if ListType[i] = 'I'  then Add(ListChamp[i], ftInteger,0, False);
                   if ListType[i] = 'D'  then Add(ListChamp[i], ftDate,0, False);
                   if ListType[i] = 'T'  then Add(ListChamp[i], ftTime,0, False);
                   if ListType[i] = 'S'  then Add(ListChamp[i], ftSmallint,0, False);
                   if ListType[i] = '@'  then Add(ListChamp[i], ftDateTime,0, False);
                   if ListType[i] = '$'  then Add(ListChamp[i], ftCurrency,0, False);
                   if ListType[i] = 'N'  then Add(ListChamp[i], ftFloat,0, False);
                   if ListType[i] = 'L'  then Add(ListChamp[i], ftBoolean,0, False);
                   if ListType[i] = 'Y'  then Add(ListChamp[i], ftBytes,0, False);
                   if ListType[i] = 'B'  then Add(ListChamp[i], ftVarBytes,0, False);
                   if ListType[i] = '+'  then Add(ListChamp[i], ftAutoInc ,0, False);
              end;
         end;
         with IndexDefs do
         begin
             Clear;
             TousChamps := ListeDesIndexTableTempo;
             {Le premier index n'a pas de nom: il s'agit d'une clé primaire Paradox  }
             if TousChamps <> '' then
                Add('',TousChamps, [ixprimary,ixunique]);
         end;
         CreateTable;
    end;
end;
 
Function TTrierForm.ListeDesIndexTableTempo : string;
var i : integer;
    TousChamps : string;
begin
     For i := 0 to ListIndex.Count -1 do
         TousChamps := TousChamps + ListIndex[i]+ ';';
     Delete(TousChamps,length(TousChamps),1);
     result := TousChamps;
end;
 
procedure TTrierForm.SiModificationTableTempo(DataSet: TDataSet);
begin
     Mainform.TriParTableTempoAvecModif := true;
end;
 
 
Procedure TTrierForm.AjouterIndex(NomColonne : string);
var DiminuChamp : string;
    i : integer;
begin
     //MainForm.Lck := False;
     //MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable.IndexDefs.Update;
 
     if not MainForm.attendre then NomTableApresTrie := MainForm.Base.NomBase;
     with MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable do
     begin
           if MainForm.attendre then TableName := NomTableApresTrie;
           DiminuChamp := MainForm.NomColonne;
           active := true;
           if FieldByName(DiminuChamp).DataType = ftString then
              RechercheFauxString;
           active := false;
     end;
     if not MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].HorsAlias then
        if RechercheApplicationAlias then
           BaseOccupee := true;
     if ((TrieAvecSQL = -1) or (TrieAvecSQL = 0)) and (not BaseOccupee) then
     begin
         //***try
           if length(DiminuChamp)> 24 then
           begin
                delete(DiminuChamp,1,6);
                MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable.AddIndex(DiminuChamp,MainForm.NomColonne,[ixCaseInsensitive]);
                MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable.IndexDefs.Update;
           end
           else
           begin
                MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable.AddIndex(MainForm.NomColonne+inttostr(1),MainForm.NomColonne,[ixCaseInsensitive]);
                MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable.IndexDefs.Update;
           end;
         //***Except on exception do
            //***MainForm.Lck := true;
         //***end;
         MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable.Active  := True;
     end;
end;
 
procedure TTrierForm.RechercheFauxString;
var k : integer;
    MaxEnreg1 : integer;
begin
    stringAuSensVrai := true;
    with MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable do
    begin
         MaxEnreg := Length(FieldByName( MainForm.NomColonne).Asstring);
         If FieldByName( MainForm.NomColonne).Asstring = '' then
         begin
            first;
            while not Eof do
            begin
                 if (FieldByName(MainForm.NomColonne).Asstring <> '') or (Eof = true) then
                     Break
                 else
                    next;
            end;
         end;
         //--Try
         k := 0;
         if StrToFloatDef(FieldByName(MainForm.NomColonne).Asstring,-1) <> -1 then
         begin
            if (MainForm.NbreEnreg <= 10000) and (not MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].IsIndex) then
            begin
               stringAuSensVrai := false;
               TrieAvecSQL := 0;
               RemplirListes;
               First;
               While not Eof do
               begin
                   Next;
                   inc(k);
                   MaxEnreg1 := Length(FieldByName(MainForm.NomColonne).Asstring);
                   MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].Gauge.Progress  := 50+(round(((25*k) / MainForm.NbreEnreg )));
                   if MaxEnreg <  MaxEnreg1  then
                      MaxEnreg := MaxEnreg1;
               end;
            end
            else
            begin
               TrieAvecSQL := 1;
               stringAuSensVrai := false;
            end;
         //--except on exception do
         end
         else
            TrieAvecSQL := -1;
 
    end;
    if not stringAuSensVrai then
         if MsgDlgMultiEcran('Par défaut les données de la colonne '+ MainForm.NomColonne + ' seront triées en format numérique. ' + #13#10
            +'Voulez-vous changer et faire un tri alphanumérique ?', mtConfirmation,[Mbyes, Mbno],0) = mryes then
         begin
            TrieAvecSQL := -1;
            stringAuSensVrai := true;
         end;
end;
 
Procedure TTrierForm.Tri();
begin
     TrieAvecSQL := -1;
     BaseOccupee := false;
     MaxEnreg := 0; // On initialise à 0 cette variable qui contient le nombre de caractères
     // de l'enregistrement maximale de la colonne à trier
     //MainForm.Gauge.Progress := 20;
     MainForm.Refresh;
     with MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable do
     begin
          close;
          AjouterIndex(MainForm.NomColonne);
     end;
     MainForm.Refresh;
     if TrieAvecSQL <> 1 then
         MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].Gauge.Progress := 50;
     if not MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].HorsAlias then //29/11/2006 AS
     begin
         if not BaseOccupee then
         begin
            if (TrieAvecSQL = -1) or (TrieAvecSQL = 0) then
               UneApplicationLance()
            else
               PassageTrieEnSql;
         end
         else
         begin
               PassageTrieEnSql;
         end;
     end
     else
       PassageTrieEnSql;
end;
 
function Ttrierform.RechercheApplicationAlias : boolean;
var NomTableOriginLocal : string;
begin
    if MainForm.TableNonIndexee then
       result := true;
    NomTableOriginLocal := MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable.TableName;
    try
      MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable.RenameTable(NomTableOriginLocal);
    except on E : exception do
    begin
      if pos ('La table est occupée',E.Message) <> 0 then
      result := True;
      if MainForm.TableNonIndexee then
         MainForm.PasDeModification := true;
    end;
    end;
    //if PostesAllumees = '' then
       //result := false
    //else
       //result := true;
 
end;
 
Procedure TTrierForm.PassageTrieEnSql;
var queryTri : Tquery;
    EnsembleDesChamps, NomTable : string;
    i : integer;
    MonType : TFieldType;
    Le_Probleme : string;
begin
     NomTable := MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable.TableName;
     MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable.Active := true;
     MonType := MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable.FieldByName(MainForm.NomColonne).DataType;
     // execution de la requête de trie
     queryTri := Tquery.create(self);
     if queryTri.active then queryTri.close;
     if not MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].HorsAlias then
        queryTri.DatabaseName := MainForm.base.CheminAlias   //29/11/2006 AS on peut trier hors alias 
     else
        queryTri.DatabaseName := MainForm.CheminTable;
     queryTri.SessionName := MainForm.ModBasDonSession.SessionName;
     queryTri.sql.clear;
     if (not stringAuSensVrai) and (MonType = ftstring) then
     begin
       EnsembleDesChamps := '';
       with MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable do
       begin
            NomTable := TableName;
            Active := true;
            for i := 0 to FieldCount -1 do
            begin
                if Fields[i].FieldName = MainForm.NomColonne then
                   EnsembleDesChamps := EnsembleDesChamps + ' cast('+ MainForm.NomColonne + ' as float) ' + Fields[i].FieldName +','
                else
                    EnsembleDesChamps := EnsembleDesChamps + Fields[i].FieldName +',';
            end;
       end;
       delete (EnsembleDesChamps,(length(EnsembleDesChamps)),1);
       if pos('.',NomTable) <> 0 then
          delete(NomTable,(length(NomTable)-2),4);
       //***if not stringAuSensVrai then
       //***begin
       if MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].IsIndex then
          Mainform.PasDeModification := true;
       if not Mainform.PasDeModification then
       begin
           queryTri.SQL.Add('Select '+ MainForm.NomColonne + ' from '+ NomTable+ ' Where ' +
           (MainForm.NomColonne + ' LIKE '+ '''0%''')+' and '+ (MainForm.NomColonne +' > '+ '''0'''));
           Le_Probleme := '';
           try
             queryTri.ExecSQL;
             queryTri.Active := true;
           except on E : exception do
             Le_Probleme := E.Message;
           end;
 
           if queryTri.RecordCount > 0 then
              Mainform.PasDeModification := true;
       end;
       //***end;
     end;
     if (not stringAuSensVrai) and (MonType = ftstring) then
     begin
         queryTri.SQL.Clear;
         queryTri.SQL.Add('select '+EnsembleDesChamps+' from '
         + NomTable + ' order by '+ MainForm.NomColonne+ ';'); 
     end
     else
         queryTri.SQL.Add('select * from '
         + NomTable + ' order by '+ MainForm.NomColonne);
     i := MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].Gauge.Progress;
     if i <= 30 then
         MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].Gauge.Progress := 50;
     try
       queryTri.Prepare;
       queryTri.ExecSQL;
     except on E : exception do
       Le_Probleme := E.Message;
     end;
     if i <= 50 then
        MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].Gauge.Progress := 70;
     //Creation de la table intermédiaire
     TableTempoTrie();
     if i <= 70 then
       MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].Gauge.Progress := 80;
     //Mis à jour de la table intermediaire
     //***if (not BaseOccupee) or (MainForm.NbreEnreg >= 20000) then
     //***begin
       BatchMoveSqlVersTable.Source := queryTri;
       BatchMoveSqlVersTable.Destination := MainForm.TableTempoTri[MainForm.ActiveMDIChild.Tag -1];
       BatchMoveSqlVersTable.AbortOnKeyViol := False;
       BatchMoveSqlVersTable.Mode := batCopy;
       Screen.Cursor := crAppStart;
       try
          BatchMoveSqlVersTable.Execute;
          MainForm.TableauGestionDonneesModifieeslocal[Mainform.ActiveMDIChild.Tag -1] := true;
       except on exception do
       begin
          Screen.Cursor := crDefault;
          if Le_Probleme <> '' then
             MsgDlgMultiEcran('Le tri a échoué : Pas de mots clés dans le nom des colonnes ' + Le_Probleme +' !',mtError,[mbok],0)
          else
            MsgDlgMultiEcran('Le tri a échoué !',mtError,[mbok],0);
          exit;
       end;
       end;
       MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].Gauge.Progress := 100;
       Screen.Cursor := crDefault;
       ListType.Free;
       ListIndex.free;
       ListTaille.free;
       MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DBgridPlusieursTables.visible := True;
       MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DatasourcePlusieursTables.DataSet := MainForm.TableTempoTri[MainForm.ActiveMDIChild.Tag -1];
       MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DatasourcePlusieursTables.Enabled := true;
       MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DBgridPlusieursTables.DataSource := MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DatasourcePlusieursTables;
       MainForm.TableTempoTri[MainForm.ActiveMDIChild.Tag -1].Active := true;
       MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].MessageDePatience(true,'');
       Mainform.Caption := 'Module de Base de Données:: Table Temporaire '+ NomTableTemporaire;  //TableOuverture.Databasename+ ' :  '+ NomTable;...
       MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DBgridPlusieursTables.SelectedField := MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable.FieldByName(MainForm.NomColonne);
       MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DBgridPlusieursTables.SelectedIndex := MainForm.Numero_Champ_Cliquer;
 
     //***end;
 
end;
 
procedure TTrierForm.RemplirListes();
var i,k : integer;
begin
      k := 0;
      ListEnregOrigine := Tstringlist.Create;
      ListEnregOrigine.Clear;
      ListChamp := Tstringlist.Create;
      ListChamp.Clear;
      with MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable do
      begin
          active := false;
          Active := true;
         IndexName := '';
          For i := 0 to FieldCount -1 do
               ListChamp.Add(Fields[i].FieldName);
          first;
          While not Eof do
          begin
           If   fieldByName(MainForm.NomColonne).value = null then
                ListEnregOrigine.Add('-')
           Else
                ListEnregOrigine.Add(fieldByName(MainForm.NomColonne).value);
                Next;
                inc(k);
                MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].Gauge.Progress  := 20+(round(((30*k) / MainForm.NbreEnreg )));
 
          end;
      end;
end;
 
// Procedure UneApplicationLance permet de trier une table quand une seule application est lancé
// dans ce cas la table n'est pas partagé
Procedure TTrierForm.UneApplicationLance();
var i,j,k : integer;
    Enreg : string;
begin
      j := 0;
      MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].Gauge.Progress := 50;
      with MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable do
      begin
           for i:=0 to IndexDefs.Count - 1 do
           begin
               if IndexDefs.Items[i].Fields = MainForm.NomColonne then
               begin
                    if FieldByName( MainForm.NomColonne).Datatype = ftString then
                    begin
                           if TrieAvecSQL = 0 then
                           begin
                               First;
                               k := 0;
                                   While not Eof do
                                   begin
                                        //Edit;
                                        if MaxEnreg > Length(FieldByName( MainForm.NomColonne).Asstring) then
                                        begin
                                           Enreg := TrierForm.ConcatenationDroiteGauche(MaxEnreg,FieldByName(MainForm.NomColonne).Asstring);
                                           Edit;
                                           FieldByName(MainForm.NomColonne).AsString:= Enreg;
                                        end;
                                        //**Post;
                                        Next;
                                        inc(k);
                                        MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].Gauge.Progress  := 75+(round(((25*k) / MainForm.NbreEnreg )));
                                   end;
                               //end;
                             //--except on exception do
                                   //--inc(j);
                             //--end;
                           end;
                           //***IndexName := IndexDefs.Items[i].Name;
                           IndexFieldNames :=  IndexDefs.Items[i].Fields;
                    end
                    else //***IndexName := IndexDefs.Items[i].Name;
                        IndexFieldNames :=  IndexDefs.Items[i].Fields;
               end;
           end;
           MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].Gauge.Visible := False;
           MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].MessageDePatience(true,'');
           MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DBgridPlusieursTables.visible := True;
           MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DatasourcePlusieursTables.DataSet := MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable;
           MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DBgridPlusieursTables.DataSource := MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DatasourcePlusieursTables;
           MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable.First;
           MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DBgridPlusieursTables.SelectedField := MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable.FieldByName(MainForm.NomColonne);
           for i := 0 to mainform.MDIChildCount -1 do
               if MainForm.MDIChildren[i].Tag = -1 then
                  MainForm.MDIChildren[i].close
               else
                   MainForm.MDIChildren[i].OnActivate(nil);
           for i := 0 to mainform.MDIChildCount -1 do
               if  mainform.MDIChildren[i] = MainForm.ActiveMDIChild then
               begin
                   MainForm.MDIChildren[i].OnActivate(nil);
                   break;
               end;
      end;
      MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DBgridPlusieursTables.SetFocus;
      try
         MainForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].DBgridPlusieursTables.SelectedIndex := MainForm.Numero_Champ_Cliquer;
      except on exception do end;
      //inForm.MultiTableForm[MainForm.ActiveMDIChild.Tag -1].UneTable.IndexName := TmpIndxName;
end;