Autor Beitrag
Zemke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 83

WIN XP
Delphi 4 Professionell
BeitragVerfasst: Fr 29.06.07 17:31 
Ich habe folgendes Problem:

Ich möchte eine Tabelle mit zwei Variablen abfragen. Hierzu möchte ich n i c h t (!) einen SQL-Befehl verwenden, sondern den Filter-Befehl der Tabelle.
Hier könnt Ihr meinen bisherigen Quellcode sehen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.Button18Click(Sender: TObject);
var
a,b:string;
begin
a:=edit1.text;
b:=dbedit1.text;
//table1.filter:='PLZ='#39+a+#39'';                       -> funktioniert
table1.filter:='PLZ='#39+a+#39 and 'Typ='#39'+b+'#39'';   -> funktioniert nicht (!?)
end;

Bei Typ handelt es sich um Text, nicht um Zahlen (Briefadresse oder Firmenadresse -> B oder F)

Was mache ich falsch ?

Moderiert von user profile iconUGrohne: Delphi-Tags hinzugefügt
PC-John
Hält's aus hier
Beiträge: 15



BeitragVerfasst: Fr 29.06.07 18:45 
Ich habe am selben Problem auch schon erfolglos herumgebissen.

Der Workaround:
Die beiden Variablen vorher "zusammenschrauben".

Die Filter-Bedingung kann nur mit einer einzigen Variablem umgehen.

Probiere mal!
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Fr 29.06.07 18:50 
Schonmal mit Klammern versucht?
wwwdirk
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 38


D6 Prof / D2005 Prof
BeitragVerfasst: Mi 11.07.07 09:11 
Zitat:
table1.filter:='PLZ='#39+a+#39 and 'Typ='#39'+b+'#39''


Das sieht auch irgendwie merkwürdig aus, das "and" scheint ja gar nicht zum String zu gehören.

Ich würd's so machen:

ausblenden Delphi-Quelltext
1:
table1.filter:= format ('PLZ=''%s'' and Typ=''%s''', [a,b]);					
noidic
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 851

Win 2000 Win XP Vista
D7 Ent, SharpDevelop 2.2
BeitragVerfasst: Mi 11.07.07 11:11 
Ich hab bei Filtern die Erfahrung gemacht, dass man am besten jede einzelne Bedingung klammert, also '(Feld1 = Wert) and (Feld2=Wert)'

_________________
Bravery calls my name in the sound of the wind in the night...
raiguen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 374

WIN 2000prof, WIN XP prof
D7EP, MSSQL, ABSDB
BeitragVerfasst: Mi 11.07.07 20:11 
user profile iconwwwdirk hat folgendes geschrieben:
Zitat:
table1.filter:='PLZ='#39+a+#39 and 'Typ='#39'+b+'#39''


Das sieht auch irgendwie merkwürdig aus, das "and" scheint ja gar nicht zum String zu gehören.

In der Tat müsste der Filterstring so aussehen:
ausblenden Delphi-Quelltext
1:
table1.filter := 'PLZ=' #39 + a + #39 + ' and Typ='#39' + b +  #39					

Besser ist aber auf jeden Fall der Vorschlag von user profile iconwwwdirk in Verbindung mit dem Tipp von user profile iconnoidichat :D dient auch der besseren Lesbarkeit/Wartbarkeit des Codes :wink: