Autor Beitrag
D. Annies
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Mo 11.02.08 11:42 
Hi, Delpher,

gibt es eine Möglichkeit, diese beiden - sehr ähnlichen - Prozeduren "zusammenzufassen", also den Code zu optimieren?

ausblenden volle Höhe Delphi-Quelltext
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:
procedure TForm1.heuteGeburtstag1Click(Sender: TObject);
var mo, tg : integer;
begin
  QSchueler.close;                        //"", "buecher", "schuelertss"
    QSchueler.DatabaseName  := listbox8.Items[listbox8.itemindex];
    if QSchueler.DatabaseName = '' then
    begin
      QSchueler.sql.text := format('select Name, Vorname, Klasse, Gebdat, Telefon from "%s" ' +
                                   ' where GEBDAT LIKE :DATUM order by Klasse', [TbSchueler.tablename]);
      QSchueler.ParamByName('DATUM').asstring := maskedit1.editText+'%';
    end
    else
    begin
      Mo := strtoint(copy(maskedit1.editText,4,2));
      Tg := strtoint(copy(maskedit1.editText,1,2));
      QSchueler.sql.text :=
        format('select Name, Vorname, Klasse, Telefon, Gebdat from "%s" ' +
               'where MONTH(gebdat) = :MONAT and DAY(gebdat) = :TAG ' +
               'order by Klasse', [TbSchueler.tablename]);
      QSchueler.ParamByName('Monat').asinteger := Mo;
      QSchueler.ParamByName('Tag').asinteger := Tg;
    end;
  QSchueler.open;
  TeilMengenausgabeSG(QSchueler);
  if form1.tntstringgrid1.cells[1,1] <> '' then   //RowCount > 1 then
  begin
    checkbox11.Checked := true; checkbox11click(self);
    fname := concat(label30.caption, 'OUTXLS\Geburtstag_S_' +inttostr(t)+inttostr(m)+ '.xls');
    application.MessageBox(#13+ '--Congratulation!--', 'Geburtstagsliste');
    checkbox11.Checked := false; checkbox11click(self);
  end
  else application.MessageBox('-Heute hat keine Person (S) Geburtstag!-', 'Geburtstagsliste');
  MaskEdit1.Text := format('%2d', [t]) + '.' + format('%2d', [m]) + '.';
  QSchuelersort(self);
end;

procedure TForm1.heuteGeburtstagLKClick(Sender: TObject);
var mo, tg : integer;
begin
  QLk.close;
    QLk.DatabaseName  := listbox8.Items[listbox8.itemindex];
    if QLk.DatabaseName = '' then
    begin
      QLk.sql.text := format('select Name, Vorname, Gebdat, Telefon from "%s" ' +
                                   ' where GEBDAT LIKE :DATUM order by Name', [TbLk.tablename]);
      QLk.ParamByName('DATUM').asstring := maskedit1.editText+'%';
    end
    else
    begin
      Mo := strtoint(copy(maskedit1.editText,4,2));
      Tg := strtoint(copy(maskedit1.editText,1,2));
      QLk.sql.text :=
        format('select Name, Vorname, Telefon, Gebdat from "%s" ' +
               'where MONTH(gebdat) = :MONAT and DAY(gebdat) = :TAG ' +
               'order by Name', [TbLk.tablename]);
      QLk.ParamByName('Monat').asinteger := Mo;
      QLk.ParamByName('Tag').asinteger := Tg;
    end;
  QLk.open;
  TeilMengenausgabeSG(QLk);
  if form1.tntstringgrid1.cells[1,1] <> '' then  //RowCount > 1 then
  begin
    checkbox11.Checked := true; checkbox11click(self);
    fname := concat(label30.caption, 'OUTXLS\Geburtstag_L_' +inttostr(t)+inttostr(m)+ '.xls');
    application.MessageBox(#13+ '--Congratulation!--', 'Geburtstagsliste');
    checkbox11.Checked := false; checkbox11click(self);
  end
  else  application.MessageBox('-Heute hat keine Person (L) Geburtstag!-', 'Geburtstagsliste');
  MaskEdit1.Text := format('%2d', [t]) + '.' + format('%2d', [m]) + '.';
  name1click(self);
end;


Vielen Dank für eure Hilfestellung, sagt
Detlef A.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 11.02.08 13:31 
Wenn du eine neue Prozedur erstellst, in der du das machst, dann musst du ja nur die SQL-Abfragen zusammensetzen je nachdem welche der beiden Sachen ausgeführt werden soll. QSchueler bzw. QLk kannst du als Parameter an die Prozedur übergeben, zusammen mit einem boolschen Switch, der dir anzeigt, wie die SQL-Abfrage aussehen muss.
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Mo 11.02.08 16:09 
Hi, Sebastian,

meine formalen Überlegungen gingen auch schon in diese Richtung. Ich habe dann im Ergebnis wohl zwei "kleine" Prozeduren und eine "große", welche das bei beiden gleiche Vorgehen abhandelt. Über den booleschen Switch muss ich noch nachdenken.

Vielleicht hast du dazu eine Idee; ich melde mich auf jeden Fall, wenn ich es gelöst habe.

Danke für deine Idee,
Detlef
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 11.02.08 17:01 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TForm1.DoIt(MyQuery: TQuery; uWithKlasse: Boolean);
// Ist das TQuery? Ich hab die Delphi-internen DB-Komponenten fast nie benutzt.
// Sonst musst du den Parameter anpassen.
begin
  MyQuery.DatabaseName  := listbox8.Items[listbox8.itemindex];
  if MyQuery.DatabaseName = '' then
  begin
    if uWithKlasse then
      MyQuery.sql.text := format('select Name, Vorname, Klasse, Gebdat, Telefon from "%s" ' +
        ' where GEBDAT LIKE :DATUM order by Klasse', [TbSchueler.tablename])
    else
      MyQuery.sql.text := format('select Name, Vorname, Gebdat, Telefon from "%s" ' +
        ' where GEBDAT LIKE :DATUM order by Name', [TbSchueler.tablename]);
    MyQuery.ParamByName('DATUM').asstring := maskedit1.editText + '%';
etc.
Eigentlich musst du die variablen Teile eben nur entsprechend mit if..else abhandeln und die Komponente, die benutzt werden soll, übergeben, das ist alles ;-).
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Mo 11.02.08 23:35 
Hi, Sebastian, ich denke, jetzt blicke ich durch!

Dann ist es ja doch nur eine einzige Prozedur, richtig?
Morgen code ich es ein.

Grüße aus Lübeck, Detlef
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 11.02.08 23:38 
Ja, eine Prozedur, die du aus den einzelnen Clicks nur aufrufen musst, dort steht also nur eine Zeile, wobei du auch theoretisch beide Klicks zusammenfassen kannst und alles komplett in eine Click-Methode für beide Komponenten packen kannst. Aber das könnte dann unübersichtlich für dich werden ;-).
D. Annies Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Di 12.02.08 14:52 
Hi, Sebastian (und andere)

hier ist jetzt meine Lösung:

ausblenden volle Höhe Delphi-Quelltext
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:
procedure TForm1.GebTag(Q_sl: TQuery; pers: Char);
var mo, tg : integer;
begin
  Mo := strtoint(copy(maskedit1.editText,4,2));
  Tg := strtoint(copy(maskedit1.editText,1,2));
  Q_sl.close;
  Q_sl.DatabaseName  := listbox8.Items[listbox8.itemindex];
  if Q_sl.DatabaseName = '' then
  begin
    if pers = 'S' then
      Q_sl.sql.text := format('select Name, Vorname, Klasse, Gebdat, Telefon from "%s" ' +
        ' where GEBDAT LIKE :DATUM order by Klasse', [TbSchueler.tablename])
    else
      Q_sl.sql.text := format('select Name, Vorname, Gebdat, Telefon from "%s" ' +
        ' where GEBDAT LIKE :DATUM order by Name', [TbLk.tablename]);
    Q_sl.ParamByName('DATUM').asstring := maskedit1.editText + '%';
  end
  else    //"", "buecher", "schuelertss"
  begin
    if pers = 'S' then
      Q_sl.sql.text :=
        format('select Name, Vorname, Klasse, Telefon, Gebdat from "%s" ' +
               'where MONTH(gebdat) = :MONAT and DAY(gebdat) = :TAG ' +
               'order by Klasse', [TbSchueler.tablename])
    else
      Q_sl.sql.text :=
        format('select Name, Vorname, Telefon, Gebdat from "%s" ' +
               'where MONTH(gebdat) = :MONAT and DAY(gebdat) = :TAG ' +
               'order by Name', [TbLk.tablename]);
    Q_sl.ParamByName('Monat').asinteger := Mo;
    Q_sl.ParamByName('Tag').asinteger := Tg;
  end;
  Q_sl.open;
  TeilMengenausgabeSG(Q_sl);
  if form1.tntstringgrid1.cells[1,1] <> '' then  //RowCount > 1 then
  begin
    if pers = 'S' then
      fname := concat(label30.caption, 'OUTXLS\Geburtstag_S_' +inttostr(tg)+inttostr(mo)+ '.xls')
    else
      fname := concat(label30.caption, 'OUTXLS\Geburtstag_L_' +inttostr(tg)+inttostr(mo)+ '.xls');
    checkbox11.Checked := true; checkbox11click(self);
      application.MessageBox(#13+ '--Congratulation!--', 'Geburtstagsliste');
    checkbox11.Checked := false; checkbox11click(self);
  end
  else  application.MessageBox('-Heute hat keine Person Geburtstag!-', 'Geburtstagsliste');
  MaskEdit1.Text := format('%2d', [t]) + '.' + format('%2d', [m]) + '.';
  if pers = 'S' then qschuelersort(self) else name1click(self);
end;


Schönen Dank für deine Anregung und dein Mutmachen!
Detlef