ErrorInfo: IExpressionErrorInfo;
The ErrorInfo property returns information on errors that may occur when parsing an expression.
Executing the example requires a form, two buttons on this form and named Button1 and Button2 and also the ExpressionEdit component named ExpressionEdit1. The repository contains a table with the Table_1 identifier. The last field of the table is a calculated field.
Class TestForm: Form
Button1: Button;
Button2: Button;
ExpressionEdit1: ExpressionEdit;
MB: IMetabase;
Tab: ITable;
Field: ITableField;
Sub Button1OnClick(Sender: Object; Args: IMouseEventArgs);
Begin
MB := MetabaseClass.Active;
Tab := MB.ItemById("Table_1").Edit As ITable;
Field := Tab.Fields.Item(Tab.Fields.Count - 1);
ExpressionEdit1.Expression := Field.Expression;
End Sub Button1OnClick;
Sub Button2OnClick(Sender: Object; Args: IMouseEventArgs);
Var
Expr: IExpression;
ErrInfo: IExpressionErrorInfo;
Begin
Expr := ExpressionEdit1.Expression;
Expr.ThrowException := False;
Expr.AsString := ExpressionEdit1.Formula;
If Not Expr.Valid Then
ErrInfo := Expr.ErrorInfo;
WinApplication.ErrorBox("On expression parsing the error occurred: "
+ ErrInfo.ErrorMessage + "." + #10 + #13 + "Position: " + ErrInfo.Position.ToString);
Else
Field.Expression.AsString := Expr.AsString;
(Tab As IMetabaseObject).Save;
End If;
End Sub Button2OnClick;
End Class TestForm;
On executing the form the ExpressionEdit1 component is connected to the expression of the calculated field of the table after the Button1 is clicked. After the expression is edited in the ExpressionEdit1 component and clicked the Button2, the system checks if the expression is valid to be used in the calculated table field. If the expression is valid, it is updated in the calculated field and the table is saved. If the expression is invalid, an error message is displayed indicating text and the number of the position where the error occurred on parsing the expression. No platform exception is thrown if the expression is invalid.
See also: