Setting Up and Applying Validation Rule

Executing the example requires the following objects in the repository:

Add links to the following system assemblies:

Example

Create a form and add the following components to it:

Sub Button1OnClick(Sender: Object; Args: IMouseEventArgs);
Var
    Mb: IMetabase;
    RubrKey: Integer;
    ValidObj: IMetabaseObject;
    ValidFilter: IValidationFilter;
    DateSettings: IValidationDateSettings;
    FilterSett: IValidationFilterSettings;
    Level: IValidationLevel;
    ComparisonValue: IValidationComparisonValue;
    ValidExecSett: IValidationExecuteSettings;
    Analyzer: IEaxAnalyzer;
    ValidExecRun: IValidationExecRun;
    DiagRep: IDiagnosticReport;
Begin
    Mb := MetabaseClass.Active;
    // Get a validation rule
    RubrKey := Mb.GetObjectKeyById("TSDB");
    ValidObj := Mb.ItemByIdNamespace("NEWVALRULE", RubrKey).Edit;
    ValidFilter := ValidObj As IValidationFilter;
    // Set start and end dates of rule calculation
    DateSettings := ValidFilter.StartDateSettings;
    DateSettings.DateOptions := ValidationDateOptions.DependsFromData;
    DateSettings := ValidFilter.EndDateSettings;
    DateSettings.DateOptions := ValidationDateOptions.DependsFromData;
    // Set calendar level to calculate the rule
    ValidFilter.Level := DimCalendarLevel.Year;
    // Set threshold value for rule exceptions
    ValidFilter.ExceptionsLimit := 500000;
    // Set parameters for calculation of the rule
    FilterSett := ValidFilter.Settings;
    // Set the rule type
    ValidFilter.Kind := ValidationDetailsKind.Level;
    Level := ValidFilter.Details As IValidationLevel;
    // Set conditions of the rule
    ComparisonValue := Level.ComparisonValue;
    ComparisonValue.ComparisonOperator := ValidationComparisonOperator.More;
    ComparisonValue.Percentage := False;
    ComparisonValue.Value1 := 100;
    // Save changes in the rule
    ValidObj.Save;
    // Execute the rule in the loaded workbook
    ValidExecSett := New ValidationExecuteSettings.Create;
    Analyzer := UiErAnalyzer1.ErAnalyzer;
    ValidExecSett.Laner := Analyzer.Laner;
    ValidExecRun := ValidFilter.Execute(ValidExecSett);
    DiagRep := New DiagnosticReport.Create;
    DiagRep.Run := ValidExecRun;
    DiagRep.EaxAnalyzer := Analyzer;
End Sub Button1OnClick;

After executing the example, on clicking the button, values greater than 100 are highlighted in red in the table.

See also:

Examples | Creating a Validation Rule