Error detection and feedback

greenspun.com : LUSENET : OCI Best Practices : One Thread

TELLS USER ABOUT FIRST ERROR



procedure TCompanyContactDlg.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin

if (ModalResult = mrCancel) then exit;

if ((DBFieldComboBoxStateCode.ItemIndex = -1) and CheckBox_address.checked) then raise EOCIWinControlError.Create('No state selected.', 'Please select a state code.', DBFieldComboBoxStateCode, 0);

if (DBFieldComboBoxCodeType.ItemIndex = -1) then raise EOCIWinControlError.Create('No address type selected.', 'Please select an address type code.', DBFieldComboBoxCodeType, 0);

if not(ValidZIP5(Edit_zip5.Text)) then raise EOCIWinControlError.Create('Invalid ZIP code.', 'Please enter a five digit number.', Edit_zip5, 0);

if not(ValidZIP4(Edit_zip4.Text)) then raise EOCIWinControlError.Create('Invalid ZIP+4 code.', 'Please enter a four digit number.', Edit_zip4, 0);

end;

TELLS USER ABOUT ALL ERRORS


procedure TCompanyContactDlg.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var e: EOCIWinControlError;
var s: string;

procedure AddError(aMessage: string; aWinControl: TWinControl); begin if not(Assigned(e)) then e := EOCIWinControlError.Create('', '', aWinControl, 0); s := s+#10#13+aMessage; end;

begin

if (ModalResult = mrCancel) then exit;

e := NIL; s := '';

if (DBFieldComboBoxCodeType.ItemIndex = -1) then AddError('No address type selected. Please select an address type code.', DBFieldComboBoxCodeType);

if ((DBFieldComboBoxStateCode.ItemIndex = -1) and CheckBox_address.checked) then AddError('No state selected. Please select a state code.', DBFieldComboBoxStateCode);

if not(ValidZIP5(Edit_zip5.Text)) then AddError('Invalid ZIP code. Please enter a five digit number.', Edit_zip5);

if (Edit_zip4.Text <> '') and not(ValidZIP4(Edit_zip4.Text)) // Zip4 is optional then AddError('Invalid ZIP+4 code. Please enter a four digit number.', Edit_zip4);

if Assigned(e) then begin e.Message := Trim(s); raise e; end;

end;



-- Anonymous, January 28, 1999

Moderation questions? read the FAQ