The following syntax shows a WITH-DO statement.

  CopyCode imageCopy Code
WITH <Record> DO
  <Statement>

When you work with records, addressing is created as record name, dot (period), and field name:

<Record>.<Field>

If you work continuously with the same record, then you can use WITH statements. When you use a WITH statement, you only specify the record name once.

Within the scope of <Statement>, fields in <Record> can be addressed without having to specify the record name.

You can nest several WITH statements. If you have identical names, then the inner WITH statement overrules the outer WITH statement.

Example

This example shows two ways to write the same code that creates a record variable that you can commit later.

Create the following variable.

Variable Data type Subtype

CustomerRec

Record

Customer

  CopyCode imageCopy Code
CustomerRec."No." := '1234';
CustomerRec.Name := 'Windy City Solutions';
CustomerRec."Phone No." := '555-444-333';
CustomerRec.Address := '1241 Druid Avenue';
CustomerRec.City := 'Windy City';
MESSAGE('A variable has been created for this customer.');

The following example shows another way to create a record variable that you can commit later

  CopyCode imageCopy Code
WITH CustomerRec DO BEGIN
  "No." := '1234';
  Name := 'Windy City Solutions';
  "Phone No." := '555-444-333';
  Address := '1241 Druid Avenue';
  City := 'Windy City';
  MESSAGE('A variable has been created for this customer.');
END;

Programming Conventions

Within WITH-DO blocks, do not repeat the name of the object with the member variable or function.

If you nest a WITH-DO block within another explicit or implicit WITH-DO block, then the WITH-DO block that you create within another WITH-DO block must always be attached to a variable of the same type as the variable that is attached to the surrounding WITH-DO block. Otherwise, it can be difficult to see what variable that a member variable or function refers to. For example, implicit WITH-DO blocks exist in table objects and in forms that have been attached to a record.

Example

The following example demonstrates nested WITH-DO blocks. Both WITH-DO blocks are attached to a Customer Ledger Entry record variable.

  CopyCode imageCopy Code
WITH CustLedgEntry DO BEGIN
  INSERT;
  ...;
  WITH CustLedgEntry2 DO BEGIN
	INSERT;
	...;
  END;
END;

Incorrect Example

The following example demonstrates incorrect code in which you cannot directly tell which record variable that the MyField field refers to.

  CopyCode imageCopy Code
WITH CustLedgEntry DO BEGIN
  ...;
  WITH VendLedgEntry DO BEGIN
	MyField := <Some Value>;
	...;
  END;
END;

See Also