請教一個組合查詢功能的代碼問題 |
答題得分者是:P.D.
|
lzkevin
一般會員 ![]() ![]() 發表:13 回覆:14 積分:5 註冊:2008-07-28 發送簡訊給我 |
請教一個組合查詢功能的代碼問題
procedure TForm2.BitBtn1Click(Sender: TObject); var str,str1,str2,str3,str4:string; begin str2:=trim(ComboBOX1.Text); Str3:=comboBox2.Text; str4:=trim(Edit1.Text); str1:=''''; str:='select * from user_base where ' str2 '' str3 '' str1 str4 str1; Adoquery1.Close; Adoquery1.SQL.Clear; Adoquery1.SQL.Add(str); Adoquery1.Open; close; end; 問題: 當ComBobox3.text等於'and'或'or'時,再加上像上面一組的代碼, //此時str2:=trim(combobx4.text),str3:=combobx5.text,str4:=tirm(Edit2.text); 當ComBobox6.text等於'and'或'or'時,再加上像上面一組的代碼, //此時str2:=trim(combobx7.text),str3:=combobx8.text,str4:=tirm(Edit3.text); 這樣就構成了一個組合查詢, 請問要怎樣實現,謝謝!!! 編輯記錄
lzkevin 重新編輯於 2008-11-07 09:58:48, 註解 無‧
|
herbert2
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:58 回覆:640 積分:894 註冊:2004-04-16 發送簡訊給我 |
|
christie
資深會員 ![]() ![]() ![]() ![]() ![]() 發表:30 回覆:299 積分:475 註冊:2005-03-25 發送簡訊給我 |
下面是where子句寫法的一個範例
Unit1.dfm [code delphi] object Form1: TForm1 Left = 192 Top = 114 Width = 696 Height = 480 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnClick = FormClick PixelsPerInch = 96 TextHeight = 13 object ComboBox1: TComboBox Left = 200 Top = 48 Width = 145 Height = 28 Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -16 Font.Name = 'MS Sans Serif' Font.Style = [] ItemHeight = 20 ParentFont = False TabOrder = 0 Text = 'ComboBox1' end object ComboBox2: TComboBox Left = 360 Top = 48 Width = 57 Height = 28 Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -16 Font.Name = 'MS Sans Serif' Font.Style = [] ItemHeight = 20 ItemIndex = 0 ParentFont = False TabOrder = 1 Items.Strings = ( '=' '>' '<') end object Memo1: TMemo Left = 160 Top = 96 Width = 497 Height = 161 Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -16 Font.Name = 'MS Sans Serif' Font.Style = [] ParentFont = False TabOrder = 2 end object RadioGroup1: TRadioGroup Left = 24 Top = 32 Width = 121 Height = 105 Caption = 'RadioGroup1' ItemIndex = 0 Items.Strings = ( 'AND' 'OR') TabOrder = 3 end object Edit1: TEdit Left = 432 Top = 48 Width = 209 Height = 28 Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -16 Font.Name = 'MS Sans Serif' Font.Style = [] ParentFont = False TabOrder = 4 Text = 'Edit1' end object BitBtn1: TBitBtn Left = 160 Top = 272 Width = 75 Height = 25 Caption = 'BitBtn1' TabOrder = 5 OnClick = BitBtn1Click end end [/code] Unit1.pas [code delphi] unit where_U; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Buttons; type TForm1 = class(TForm) ComboBox1: TComboBox; ComboBox2: TComboBox; Memo1: TMemo; RadioGroup1: TRadioGroup; Edit1: TEdit; BitBtn1: TBitBtn; procedure FormClick(Sender: TObject); procedure BitBtn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormClick(Sender: TObject); begin Memo1.Lines.Clear end; procedure TForm1.BitBtn1Click(Sender: TObject); begin if Memo1.Lines.Count>0 then begin case RadioGroup1.ItemIndex of 0: Memo1.Lines.Add('AND'); 1: Memo1.Lines.Add('OR') end; end; Memo1.lines.add(format('(%s %s ''%s'')',[ComboBox1.Text,ComboBox2.Text,Edit1.Text ])) end; end. [/code] 您可以自己延伸。 ===================引 用 lzkevin 文 章=================== 請教一個組合查詢功能的代碼問題 procedure TForm2.BitBtn1Click(Sender: TObject); var str,str1,str2,str3,str4:string; begin str2:=trim(ComboBOX1.Text); Str3:=comboBox2.Text; str4:=trim(Edit1.Text); str1:=''''; str:='select * from user_base where ' str2 '' str3 '' str1 str4 str1; Adoquery1.Close; Adoquery1.SQL.Clear; Adoquery1.SQL.Add(str); Adoquery1.Open; close; end; 問題: 當ComBobox3.text等於'and'或'or'時,再加上像上面一組的代碼, //此時str2:=trim(combobx4.text),str3:=combobx5.text,str4:=tirm(Edit2.text); 當ComBobox6.text等於'and'或'or'時,再加上像上面一組的代碼, //此時str2:=trim(combobx7.text),str3:=combobx8.text,str4:=tirm(Edit3.text); 這樣就構成了一個組合查詢, 請問要怎樣實現,謝謝!!!
------
What do we live for if not to make life less difficult for each other? |
P.D.
版主 ![]() ![]() ![]() ![]() ![]() ![]() 發表:603 回覆:4038 積分:3874 註冊:2006-10-31 發送簡訊給我 |
這是我現在還在用的土法, 很好用, 很容易懂, 沒有技巧, 唯一的缺點是組合條件越多, 程式碼越長, 參考看看
![]() [code delphi] procedure TForm_Rep532.btn_FilterClick(Sender: TObject); var ft1, ft2, ft3, ft4, ft5, ft6, ft7: string; fta: string; ft6str: string; morder, msql: string; begin SysUt.MsgDialog('銷貨資料調閱中....'); ft1:= ''; ft2:= ''; ft3:= ''; ft4:= ''; ft5:= ''; ft6:= ''; ft7:= ''; fta:= ''; // 貨倉(ismaster是為解決如果使用還原鍵時) if ismaster then if cbxDept.Text<>'' then ft1:= 'DEPTNO="' cbxDept.Text '"'; // 品牌 if cbxBrand.Text<>'' then ft2:= 'BRANDNO="' cbxBrand.Text '"'; // 銷貨日期 if (trim(eD1.Text)<>'') and (trim(eD2.Text)='') then ft3:= 'FORMDATE>="' eD1.Text '"' else if (trim(eD1.Text)='') and (trim(eD2.Text)<>'') then ft3:= 'FORMDATE<="' eD2.Text '"' else if (trim(eD1.Text)<>'') and (trim(eD2.Text)<>'') then ft3:= '(FORMDATE>="' eD1.Text '") and ' '(FORMDATE<="' eD2.Text '")'; // 客戶編號 if (trim(eC1.Text)<>'') and (trim(eC2.Text)='') then ft4:= 'VIPNO>="' eC1.Text '"' else if (trim(eC1.Text)='') and (trim(eC2.Text)<>'') then ft4:= 'VIPNO<="' eC2.Text '_"' else if (trim(eC1.Text)<>'') and (trim(eC2.Text)<>'') then ft4:= '(VIPNO>="' eC1.Text '") and ' '(VIPNO<="' eC2.Text '_")'; // 群組 if eG1.Text<>'' then ft5:= '(GROUPS like "' eG1.Text '%")'; if eG2.Text<>'' then begin if ft5='' then ft5:= '(GROUPS like "' eG2.Text '%")' else ft5:= ft5 ' or (GROUPS like "' eG2.Text '%")'; end; if eG3.Text<>'' then begin if ft5='' then ft5:= '(GROUPS like "' eG3.Text '%")' else ft5:= ft5 ' or (GROUPS like "' eG3.Text '%")'; end; if eG4.Text<>'' then begin if ft5='' then ft5:= '(GROUPS like "' eG4.Text '%")' else ft5:= ft5 ' or (GROUPS like "' eG4.Text '%")'; end; if eG5.Text<>'' then begin if ft5='' then ft5:= '(GROUPS like "' eG5.Text '%")' else ft5:= ft5 ' or (GROUPS like "' eG5.Text '%")'; end; // 計算憑單 ft6str:= ''; if eF1.Text<>'' then ft6str:= '"' eF1.Text '"'; if eF2.Text<>'' then begin if ft6str='' then ft6str:= '"' eF2.Text '"' else ft6str:= ft6str ',"' eF2.Text '"'; end; if eF3.Text<>'' then begin if ft6str='' then ft6str:= '"' eF3.Text '"' else ft6str:= ft6str ',"' eF3.Text '"'; end; if eF4.Text<>'' then begin if ft6str='' then ft6str:= '"' eF4.Text '"' else ft6str:= ft6str ',"' eF4.Text '"'; end; if eF5.Text<>'' then begin if ft6str='' then ft6str:= '"' eF5.Text '"' else ft6str:= ft6str ',"' eF5.Text '"'; end; if eF6.Text<>'' then begin if ft6str='' then ft6str:= '"' eF6.Text '"' else ft6str:= ft6str ',"' eF6.Text '"'; end; if eF7.Text<>'' then begin if ft6str='' then ft6str:= '"' eF7.Text '"' else ft6str:= ft6str ',"' eF7.Text '"'; end; if eF8.Text<>'' then begin if ft6str='' then ft6str:= '"' eF8.Text '"' else ft6str:= ft6str ',"' eF8.Text '"'; end; if eF9.Text<>'' then begin if ft6str='' then ft6str:= '"' eF9.Text '"' else ft6str:= ft6str ',"' eF9.Text '"'; end; if eF10.Text<>'' then begin if ft6str='' then ft6str:= '"' eF10.Text '"' else ft6str:= ft6str ',"' eF10.Text '"'; end; if ft6str<>'' then ft6:= 'HEADNO in (' ft6str ')'; // 確認 if chbConf.Checked then ft7:= 'ISCONFIRM="Y"'; // 分析條件 if ft1 <> '' then fta:= '(' ft1 ')'; if ft2 <> '' then begin if fta= '' then fta:= '(' ft2 ')' else fta:= fta ' and (' ft2 ')'; end; if ft3 <> '' then begin if fta= '' then fta:= '(' ft3 ')' else fta:= fta ' and (' ft3 ')'; end; if ft4 <> '' then begin if fta= '' then fta:= '(' ft4 ')' else fta:= fta ' and (' ft4 ')'; end; if ft5 <> '' then begin if fta= '' then fta:= '(' ft5 ')' else fta:= fta ' and (' ft5 ')'; end; if ft6 <> '' then begin if fta= '' then fta:= '(' ft6 ')' else fta:= fta ' and (' ft6 ')'; end; if ft7 <> '' then begin if fta= '' then fta:= '(' ft7 ')' else fta:= fta ' and (' ft7 ')'; end; if fta<>'' then begin Frame_Qdata.SQLfilter:= ' where ' fta ' and ' '(' Frame_Qdata.SQLfilterbefore ')'; end else Frame_Qdata.SQLfilter:= ' where ' Frame_Qdata.SQLfilterbefore; Frame_Qdata.SQLorder:= ' order by GROUPS, SHORTNAME, FORMDATE, DEPTNO, HEADNO, BODYNO'; Frame_Qdata.SQLstring:= Frame_Qdata.SQLfield Frame_Qdata.SQLfilter Frame_Qdata.SQLorder; IBQ_D8000m.Close; IBQ_D8000m.SQL.Text:= Frame_Qdata.SQLstring; IBQ_D8000m.Open; end; [/code] |
lzkevin
一般會員 ![]() ![]() 發表:13 回覆:14 積分:5 註冊:2008-07-28 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |