關於function的問題想請教 |
尚未結案
|
LearnDelphi
一般會員 發表:4 回覆:7 積分:2 註冊:2005-04-07 發送簡訊給我 |
程式1 一開始寫簡單資料上下筆查詢的程式
後來想要加入function的函數
程式2 加了function宣告...
但是不能執行..請問大家..我哪裡做錯或是忽略了呢 程式1.
*********************
unit CUS01U1; interface uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, StdCtrls, Mask, DBCtrls, ExtCtrls; type
TForm1 = class(TForm)
PanFunction: TPanel;
First: TButton;
Prioor: TButton;
Next: TButton;
Last: TButton;
PanMeat: TPanel;
EditCusNo: TDBEdit;
DataSource1: TDataSource;
LabCompany: TLabel;
EditCompany: TDBEdit;
LabAddr1: TLabel;
EditAddr1: TDBEdit;
LabAddr2: TLabel;
EditAddr2: TDBEdit;
LabCity: TLabel;
EditCity: TDBEdit;
LabState: TLabel;
EditState: TDBEdit;
LabZip: TLabel;
EditZip: TDBEdit;
Labcountry: TLabel;
EditCountry: TDBEdit;
LabPhone: TLabel;
EditPhone: TDBEdit;
LabFax: TLabel;
EditFax: TDBEdit;
LabTaxRate: TLabel;
EditTaxRate: TDBEdit;
LabContact: TLabel;
EditContact: TDBEdit;
LabLastDate: TLabel;
EditLastlDate: TDBEdit;
LabCusNo: TLabel;
procedure FirstClick(Sender: TObject);
procedure PrioorClick(Sender: TObject);
procedure NextClick(Sender: TObject);
procedure LastClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation uses CUS01U2;
var CusNo : String;
{$R *.DFM} procedure TForm1.FirstClick(Sender: TObject);
begin
CusDaMd.QueryCus.Close;
CusDaMd.QueryCus.SQL.Clear;
CusDaMd.QueryCus.SQL.Add('select * from customer');
CusDaMd.QueryCus.SQL.Add('where CustNo = (select min(CustNo)from customer)');
CusDaMd.QueryCus.Open;
CusNo := CusDaMd.QueryCus.FieldByName('CustNo').AsString;
end; procedure TForm1.PrioorClick(Sender: TObject);
begin
CusDaMd.QueryCus.Close;
CusDaMd.QueryCus.SQL.Clear;
CusDaMd.QueryCus.SQL.Add('select * from customer');
CusDaMd.QueryCus.SQL.Add('where CustNo =(select max(CustNo)from customer');
CusDaMd.QueryCus.SQL.Add('where CustNo <''' CusNo ''')');
CusDaMd.QueryCus.Open;
CusNo := CusDaMd.QueryCus.FieldByName('CustNo').AsString;
end; procedure TForm1.NextClick(Sender: TObject);
begin
CusDaMd.QueryCus.Close;
CusDaMd.QueryCus.SQL.Clear;
CusDaMd.QueryCus.SQL.Add('select * from customer');
CusDaMd.QueryCus.SQL.Add('where CustNo =(select min(CustNo)from customer');
CusDaMd.QueryCus.SQL.Add('where CustNo >''' CusNo ''')');
CusDaMd.QueryCus.Open;
CusNo := CusDaMd.QueryCus.FieldByName('CustNo').AsString;
end; procedure TForm1.LastClick(Sender: TObject);
begin
CusDaMd.QueryCus.Close;
CusDaMd.QueryCus.SQL.Clear;
CusDaMd.QueryCus.SQL.Add('select * from customer');
CusDaMd.QueryCus.SQL.Add('where CustNo = (select max(CustNo)from customer)');
CusDaMd.QueryCus.Open;
CusNo := CusDaMd.QueryCus.FieldByName('CustNo').AsString;
end; end.
|
LearnDelphi
一般會員 發表:4 回覆:7 積分:2 註冊:2005-04-07 發送簡訊給我 |
程式2.(加function)
***********************
unit CUS01U1; interface uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, StdCtrls, Mask, DBCtrls, ExtCtrls; type
TForm1 = class(TForm)
PanFunction: TPanel;
First: TButton;
Prioor: TButton;
Next: TButton;
Last: TButton;
PanMeat: TPanel;
EditCusNo: TDBEdit;
DataSource1: TDataSource;
LabCompany: TLabel;
EditCompany: TDBEdit;
LabAddr1: TLabel;
EditAddr1: TDBEdit;
LabAddr2: TLabel;
EditAddr2: TDBEdit;
LabCity: TLabel;
EditCity: TDBEdit;
LabState: TLabel;
EditState: TDBEdit;
LabZip: TLabel;
EditZip: TDBEdit;
Labcountry: TLabel;
EditCountry: TDBEdit;
LabPhone: TLabel;
EditPhone: TDBEdit;
LabFax: TLabel;
EditFax: TDBEdit;
LabTaxRate: TLabel;
EditTaxRate: TDBEdit;
LabContact: TLabel;
EditContact: TDBEdit;
LabLastDate: TLabel;
EditLastlDate: TDBEdit;
LabCusNo: TLabel;
procedure FirstClick(Sender: TObject);
procedure PrioorClick(Sender: TObject);
procedure NextClick(Sender: TObject);
procedure LastClick(Sender: TObject);
function ToRecord(QueryDateSet : TQuery ; ActRecord : string ): Boolean;
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation uses CUS01U2;
var CusNo : String;
{$R *.DFM} procedure TForm1.FirstClick(Sender: TObject);
begin
ToRecord (CusDaMa.QueryCus,'FirstRun');
end; procedure TForm1.PrioorClick(Sender: TObject);
begin
ToRecord (CusDaMa.QueryCus,'PriorRun');
end; procedure TForm1.NextClick(Sender: TObject);
begin
ToRecord (CusDaMa.QueryCus,'NextRun');
end; procedure TForm1.LastClick(Sender: TObject);
begin
ToRecord (CusDaMd.QueryCus,'LastRun');
end; function TFrom1.ToRecord(QueryDateSet : TQuery ; ActRecord : String ):Boolean;
var CaseSet : String ;
begin
CaseSet := ActRecord;
CusDaMd.QueryCus.Close;
CusDaMd.QueryCus.SQL.Clear;
case CaseSet of
FirstRun : begin
CusDaMd.QueryCus.SQL.Add('select * from customer');
CusDaMd.QueryCus.SQL.Add('where CustNo = (select min(CustNo)from customer)');
end;
PriorRun : begin
CusDaMd.QueryCus.SQL.Add('select * from customer');
CusDaMd.QueryCus.SQL.Add('where CustNo =(select max(CustNo)from customer');
CusDaMd.QueryCus.SQL.Add('where CustNo <''' CusNo ''')');
end;
NextRun : begin
CusDaMd.QueryCus.SQL.Add('select * from customer');
CusDaMd.QueryCus.SQL.Add('where CustNo =(select min(CustNo)from customer');
CusDaMd.QueryCus.SQL.Add('where CustNo >''' CusNo ''')');
end;
LestRun : begin
CusDaMd.QueryCus.SQL.Add('select * from customer');
CusDaMd.QueryCus.SQL.Add('where CustNo = (select max(CustNo)from customer)');
end;
end;
CusDaMd.QueryCus.Open;
CusNo := CusDaMd.QueryCus.FieldByName('CustNo').AsString;
end; end.
|
likush
高階會員 發表:5 回覆:235 積分:103 註冊:2002-10-08 發送簡訊給我 |
小弟愚見
依您的作法,簡單點應將CusNo排序後,直接採用QueryCus.First等作法即可,再則Case不能用字串吧?如果要依您的作法來說的話應可用
function TForm1.ToRecord(QueryDateSet : TQuery ; ActRecord : Char ):Boolean;
然後
ToRecord (CusDaMa.QueryCus,'N')即可 =========================
讀萬卷書~不如來K.TOP走一遭
=========================
|
supman
尊榮會員 發表:29 回覆:770 積分:924 註冊:2002-04-22 發送簡訊給我 |
您好:
我幫您把程式整個改了一下,減少程式碼,也能達到您的需求,及沒考慮到的地方,您參考參考
procedure TForm1.zQueryWork(var Query:TAdoQuery;Str:String); begin Query.Close(); Query.SQL.Clear(); Query.SQL.Add(Str); Query.Open(); end; procedure TForm1.btnFirstClick(Sender: TObject); begin if (Sender=btnFirst) then zQueryWork(qryCust,'select * from cust where No = (select min(No)from cust)'); if (Sender=btnPrior) then zQueryWork(qryCust,'SELECT TOP 1 * FROM Cust ' 'WHERE No<''' CustNo ''' ' 'ORDER BY No DESC '); if (Sender=btnNext) then zQueryWork(qryCust,'SELECT TOP 1 * FROM Cust ' 'WHERE No>''' CustNo ''' ' 'ORDER BY No Asc '); if (Sender=btnLast) then zQueryWork(qryCust,'select * from cust where No = (select max(No)from cust)'); if (qryCust.IsEmpty) then zQueryWork(qryCust,'select * from cust where No = ''' CustNo ''''); CustNo:=qryCust.FieldByName('No').AsString; end; procedure TForm1.FormActivate(Sender: TObject); begin CustNo:='00001'; end; |
LearnDelphi
一般會員 發表:4 回覆:7 積分:2 註冊:2005-04-07 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |