13. Creating Dynamic Frames : How You Can Manipulate Data in Dynamic Fields : How You Can Access Individual Cells of a Dynamic Table Field : How You Can Get Values from Unknown Fields
 
Share this page                  
How You Can Get Values from Unknown Fields
The previous examples of getting and setting values with dynamic expressions assume you know the name and data type of the current table field column. Assume instead that you want to get a value from the current cell of a dynamic table field named tbl but you do not know which cell currently has the input focus. Assume also that all columns in the table have a data type of integer or varchar.
When you invoke the GetValue method, you must put the value obtained into a variable of the appropriate data type. Therefore, getting the value from the current cell requires using a conditional statement and creating a dynamic expression and invoking the GetValue method.
The following field script performs these operations when the user enters a cell in a table field:
declare
    txt = varchar(50) not null;
    dexp = DynExpr default null;
    errstr = StringObject default null;
    intvar = integer not null;
    textvar = varchar(50) not null;
    status = integer not null;
enddeclare
on childentry =
begin
    txt = 'tbl[].' + CurFrame.TriggerField.Name;
    dexp = CurFrame.Scope.CreateDynExpr
        (string = txt, errors = byref(errstr));
    if CurFrame.TriggerField.DataType = 'integer'
        then status = dexp.GetValue(value =
                byref(intvar));
    else
        status = dexp.GetValue
                (value = byref(textvar));
    endif;
    ...
end
After declaring the appropriate variables, this code uses the Name attribute defined for the FieldObject system class to create a text string that contains the full name of the current table field cell.
If the expression in the txt variable is valid (that is, points to an existing field and performs valid operations), the errstr variable is null after the statement creating this dynamic expression; otherwise, it contains the text of the error message.
The following code assigns a new value to the same cell in tbl, using the same dynamic expression:
intvar = intvar * 1.15;
status = dexp.SetValue(value = intvar);
You can get a value from the cell and assign a new one in a single step using the Assign method. Because the Assign method assigns the value of one dynamic expression to another, you can use this method to copy values between dynamic expressions without knowing the data type of the value in each expression.
For example, the following code creates two dynamic expressions, one containing the current value of the specified cell and the other containing that value increased by 15 percent, and then assigns the value of the second dynamic expression to the first:
txt = 'tbl[i].' + CurFrame.TriggerField.Name;
dexp_a = CurFrame.Scope.CreateDynExpr(string = txt,
    errors = byref(errstr));
txt = txt + '* 1.15';
dexp_b = CurFrame.Scope.CreateDynExpr(string = txt,
    errors = byref(errstr));
status = dexp_a.Assign(fromdynexpr = dexp_b);
Although using the Assign method in this manner requires creating two dynamic expressions, it eliminates the need for an intermediate variable.
For more information, see How You Can Create a Dynamic Expression (see How You Can Create a Dynamic Expression).