全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:880
推到 Plurk!
推到 Facebook!

How can i block Access db Exceptions Error?

尚未結案
Vashee
初階會員


發表:38
回覆:87
積分:25
註冊:2003-03-31

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-05-11 22:11:26 IP:217.36.xxx.xxx 未訂閱
say I enter data from delphi, when I enter the sample Primary Key into one table, exception error message will raise because primary key must be unique (of course :)), but exceptions will crash the whole program, how can i transform those exceptions into a Message form that will not crash the program? like a validation. Thanks
timhuang
尊榮會員


發表:78
回覆:1815
積分:1608
註冊:2002-07-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-05-11 22:25:49 IP:61.221.xxx.xxx 未訂閱
The best way is using try.. except to handle this error. If you want to validate the input data, you can do this with BeforePost event.
Vashee
初階會員


發表:38
回覆:87
積分:25
註冊:2003-03-31

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-05-11 23:09:03 IP:217.36.xxx.xxx 未訂閱
If you want to validate the input data, you can do this with BeforePost event. <--- Can u explain a bit more about this one? how show the coding looks like?
timhuang
尊榮會員


發表:78
回覆:1815
積分:1608
註冊:2002-07-15

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-05-12 01:08:51 IP:61.221.xxx.xxx 未訂閱
Add some code into BeforePost event like :    
procedure TForm1.Table1BeforePost(DataSet: TDataSet);
var
  Qry: TQuery;
begin
  If Table1.State in [dsInsert] then
  begin
    Qry:= TQuery.Create(nil);
    try
      Qry.SQL.Add('select top 1 code from tableA where col_pk=''' Table1.FieldByName('col_pk').AsString ''' ');
      Qry.DatabaseName := Table1.DatabaseName;
      Qry.Open;
      If not Qry.IsEmpty then
      Begin
        ShowMessage('the same pk data');
        Abort;
      End;
    finally
      Qry.Free;
    end;
  end;
end;
Vashee
初階會員


發表:38
回覆:87
積分:25
註冊:2003-03-31

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-05-12 03:14:57 IP:217.35.xxx.xxx 未訂閱
i think i stick with using try.. except just making sure i am doing this right.. i am trying to prevent exceptation message for duplicate primary key which is a combination of (CentreNumber, CandidateNumber) so: try tExamRecord.FieldByName('CandidateNumber').AsString := Can_No.Text; tExamRecord.FieldByName('CentreNumber').AsString := Centre_No.Text; except showmessage('This record already exist!'); but it doesn't seems to work, why?
Vashee
初階會員


發表:38
回覆:87
積分:25
註冊:2003-03-31

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-05-12 03:21:21 IP:217.35.xxx.xxx 未訂閱
begin tStudent.Append; tStudent.FieldByName('CandidateName').AsString := Can_Name.Text; try tStudent.FieldByName('CandidateNumber').AsString := Can_no.Text; tStudent.FieldByName('CentreNumber').AsString := Centre_No.Text; except ShowMessage('The record already exist'); end; tStudent.FieldByName('CentreName').AsString := Centre_Name.Text; tStudent.Post; end; This is the full coding
Vashee
初階會員


發表:38
回覆:87
積分:25
註冊:2003-03-31

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-05-12 06:17:44 IP:217.35.xxx.xxx 未訂閱
I have modiftied the coding further, but it still couldn't block the exception message, when I enter the same primary key into the table begin try tStudent.Append; tStudent.FieldByName('CandidateName').AsString := Can_Name.Text; tStudent.FieldByName('CandidateNumber').AsString := Can_no.Text; tStudent.FieldByName('CentreNumber').AsString := Centre_No.Text; tStudent.FieldByName('CentreName').AsString := Centre_Name.Text; tStudent.Post; except ShowMessage('Record Already Exist!'); end; end;
Justmade
版主


發表:94
回覆:1934
積分:2030
註冊:2003-03-12

發送簡訊給我
#8 引用回覆 回覆 發表時間:2003-05-12 07:52:46 IP:218.16.xxx.xxx 未訂閱
the exception message is debugger message but not application message in your case. Run the program from the exe (i.e. not in Delphi) and there should be no exception. Instead, your message will be shown.
系統時間:2024-11-23 2:21:55
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!