How can i block Access db Exceptions Error? |
尚未結案
|
Vashee
初階會員 發表:38 回覆:87 積分:25 註冊:2003-03-31 發送簡訊給我 |
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 發送簡訊給我 |
|
Vashee
初階會員 發表:38 回覆:87 積分:25 註冊:2003-03-31 發送簡訊給我 |
|
timhuang
尊榮會員 發表:78 回覆:1815 積分:1608 註冊:2002-07-15 發送簡訊給我 |
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 發送簡訊給我 |
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 發送簡訊給我 |
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 發送簡訊給我 |
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 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |