Page 1 of 1

Intercept/change DB error message

Posted: Tue 16 Apr 2019 15:01
by polsol
Please consider a grid with 2 columns (simplified). The first column is a 'lookup' column ("Name") while the second is a required field (ID Number).
Column A is used to select a 'Name' from a DB Lookup list and fill the associated ID Number into the second Column: "ID Number". Note that the actual value for "Name" does not have an underlying Field component in this table - column A just exists to get the ID Number and enter it in the table.

If one tries to post a record without selecting the 'Name' (and hence adding the ID number into the ID number field), one gets the error message "Field "ID Number" must have a value".
This could, of course, be confusing to the end user as the field they're entering is "Name" - not "ID Number" (especially if the ID number is not visible in the grid) ....
How can one intercept the database error mechanism to change the error message to "Field "Name" must have a value" to avoid confusion when the ID Number Field is null?

Re: Intercept/change DB error message

Posted: Tue 23 Apr 2019 10:39
by Stellar
You can try processing the BeforePost event for TDataSet and check the value of the field you're interested in. If the value is invalid, you can generate a custom error for the user. For example:

Code: Select all

procedure TForm1.UniQuery1BeforePost(DataSet: TDataSet);
begin
  if DataSet.FieldByName('Field_Name').IsNull then
    DatabaseError('Error message');
end;

Re: Intercept/change DB error message

Posted: Tue 23 Apr 2019 11:12
by polsol
Thanks Stellar,
That's what I have now started doing and it works fine.
I was thinking there might be a simpler way.

Re: Intercept/change DB error message

Posted: Wed 24 Apr 2019 15:06
by Stellar
Glad to see that the issue was resolved.
Feel free to contact us if you have any further questions about our products.