ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

  • Equivalence
  • Filter (syntaxes 2)
  • Exiting from FOR EACH loop
  • Influence of the mode for exiting from the loop on the automatic browse of data files
  • Running the next iteration
  • Modifying the key used for the automatic browse of data files or queries
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
The FOR EACH statement is used to perform different types of HFSQL browse:
  • Full browse (according to a specified key or not)
  • Browse with filter (simple filter, filter on a key or filter on the search key). In this case, the filters accept the operators of HFilter.
The browse operations can be performed on a data file, a view, a query or a data source. The records locked in read/write are not read.
Remarks:
  • If the key given to the FOR EACH statement is the key that was previously returned by HFilter, the filter will be respected.
  • The FOR ALL, FOR EACH statements are accepted. The FOR EACH statement will be used in this documentation but it can be replaced with FOR ALL.
Example
Syntax

1 - Full browse Hide the details

1. Full browse according to the best search key

Caution: Modifying the file in the analysis (adding a key item for example) can modify the search key used.

FOR EACH <File> [[WITHOUTSAVEPOSITION,] [<Direction>]]
...
END

2. Full browse according to the key passed as parameter

FOR EACH <File> ON <Key item> [[WITHOUTSAVEPOSITION,] [<Direction>]]
...
END
<FOR EACH>:
Marks the beginning of the statement block.
<File>:
Name of the HFSQL data file, view or query to browse.
<ON>:
Defines the type of browse.
<Key item>:
Name of the key item used to browse the HFSQL data file (view or query).
<WITHOUTSAVEPOSITION>:
Optional keyword.
Disables the position save and restore during the browse. Indeed, FOR EACH automatically saves the position at the beginning of the loop and restores this position in case of "normal" exit from the loop (the position is not restored if the loop is interrupted by the keywords BREAK, RETURN or RESULT).
These position save and restore operations can have a high impact (mainly in terms of time), especially if the code using the instruction FOR EACH itself is called in a loop.
<Direction>:
Optional indicator for the browse direction:
FromBeginning
(default value)
Browses the data file from the first element to the last one.
FromEndBrowses the data file from the last element to the first one.
<END>:
Marks the end of the statement block.

2 - Browse with filter Hide the details

1. Browse with filter (the best search key is automatically defined)

Caution: Modifying the file in the analysis (adding a key item for example) can modify the search key used.

FOR EACH <File> WHERE "<1st Condition>
               [AND/OR/NOT <2nd Condition>
               [ET/OU/PAS...<Nth Condition>]]" [[WIHTOUTSAVEPOSITION,] [<Direction>]]
...
END

2. Browse with filter according to the specified key

FOR EACH <File> WHERE "<1st Condition>
               [AND/OR/NOT <2nd Condition>
               [ET/OU/PAS...<Nth Condition>]]" [[WIHTOUTSAVEPOSITION,] [<Direction>]]
...
ON <Key Item>
END
<FOR EACH>:
Marks the beginning of the statement block.
<File>:
Name of the HFSQL data file, view or query to browse.
<WITH>:
Defines the type of browse.
<Nth Condition>:
Nth condition of the filter for the HFSQL browse. For more details, see Remarks.
<AND/OR/NOT>:
Optional logical operators used to combine the different filter conditions.
<WITHOUTSAVEPOSITION>:
Optional keyword.
Disables the position save and restore during the browse. Indeed, FOR EACH automatically saves the position at the beginning of the loop and restores this position in case of "normal" exit from the loop (the position is not restored if the loop is interrupted by the keywords BREAK, RETURN or RESULT).
These position save and restore operations can have a high impact (mainly in terms of time), especially if the code using the instruction FOR EACH itself is called in a loop.
<Direction>:
Optional indicator for the browse direction:
FromBeginning
(default value)
Browses the data file from the first element to the last one.
FromEndBrowses the data file from the last element to the first one.
<Key item>:
Item corresponding to a key of the data file. The filter will be performed on this key.
<END>:
Marks the end of the statement block.

3 - Browse with selection filter on the search key Hide the details

1. Comparison filter according to a value

FOR EACH <Fie> WHERE <Key item> [ = / <= / >= ] <Value> [[WIHTOUTSAVEPOSITION,] [<Direction>]]
...
END

2. Comparison filter according to an interval of values

FOR EACH <File> WHERE <Key item> = <Minimal value> TO <Maximum value> [[WITHOUTSAVEPOSITION,] [<Direction>]]
...
END

FOR EACH <File> WHERE <Minimum value> <= <Key item> <= <Maximum value> [[WIHTOUTSAVEPOSITION,] [<Direction>]]
...
END
<FOR EACH>:
Marks the beginning of the statement block.
<File>:
Name of the HFSQL data file, view or query to browse.
<WITH>:
Defines the type of browse.
<Key item>:
Key item of the data file to browse.
<Value>:
Comparison value of the key item.
<Minimum value>, <Maximum value>:
Comparison value of the key item.
<WITHOUTSAVEPOSITION>:
Optional keyword.
Disables the position save and restore during the browse. Indeed, FOR EACH automatically saves the position at the beginning of the loop and restores this position in case of "normal" exit from the loop (the position is not restored if the loop is interrupted by the keywords BREAK, RETURN or RESULT).
These position save and restore operations can have a high impact (mainly in terms of time), especially if the code using the instruction FOR EACH itself is called in a loop.
<Direction>:
Optional indicator for the browse direction:
FromBeginning
(default value)
Browses the data file from the first element to the last one.
FromEndBrowses the data file from the last element to the first one.
<END>:
Marks the end of the statement block.

4 - Browse with "Start with" generic search, ascending or descending Hide the details

FOR EACH <File> WHERE <Key item> [= <Beginning of sought value> [[WITHOUTSAVEPOSITION,] [<Direction>]]
...
END
<FOR EACH>:
Marks the beginning of the statement block.
<File>:
Name of the HFSQL data file, view or query to browse.
<WITH>:
Defines the type of browse.
<Key item>:
Key item of the data file to browse.
<Beginning of sought value>:
Comparison value of the key item.
<WITHOUTSAVEPOSITION>:
Optional keyword.
Disables the position save and restore during the browse. Indeed, FOR EACH automatically saves the position at the beginning of the loop and restores this position in case of "normal" exit from the loop (the position is not restored if the loop is interrupted by the keywords BREAK, RETURN or RESULT).
These position save and restore operations can have a high impact (mainly in terms of time), especially if the code using the instruction FOR EACH itself is called in a loop.
<Direction>:
Optional indicator for the browse direction:
FromBeginning
(default value)
Browses the data file from the first element to the last one.
FromEndBrowses the data file from the last element to the first one.
<END>:
Marks the end of the statement block.

5 - Browse with filter on a composite key Hide the details

1. Exact-match search

FOR EACH <File> WHERE <Composite key> = [<Value of component 1>, ..., <Value of component N>] [[WITHOUTSAVEPOSITION,] [<Direction>]]
...
END

2. Generic search ("Starts with")

FOR EACH <File> WHERE <Composite key> [= [<Value of component 1>, ..., <Value of component N>] [[WITHOUTSAVEPOSITION,] [<Direction>]]

...
END
<FOR EACH>:
Marks the beginning of the statement block.
<File>:
Name of the HFSQL data file or view to browse.
<WITH>:
Defines the type of browse.
<Composite key>:
Composite key of the data file to browse.
<Value of component N>:
Value of the component.
<WITHOUTSAVEPOSITION>:
Optional keyword.
Disables the position save and restore during the browse. Indeed, FOR EACH automatically saves the position at the beginning of the loop and restores this position in case of "normal" exit from the loop (the position is not restored if the loop is interrupted by the keywords BREAK, RETURN or RESULT).
These position save and restore operations can have a high impact (mainly in terms of time), especially if the code using the instruction FOR EACH itself is called in a loop.
<Direction>:
Optional indicator for the browse direction:
FromBeginning
(default value)
Browses the data file from the first element to the last one.
FromEndBrowses the data file from the last element to the first one.
<END>:
Marks the end of the statement block.
Remarks

Equivalence

  • The syntax FOR EACH <File> WHERE <Item> = <Value> is identical to looping through the data file with HReadSeek. This browse takes the filters previously defined on the data file into account.
  • The syntax FOR EACH <File> WHERE <Condition> is used to define a filter and to browse the data file. This browse ignores the filters that were defined beforehand.
  • When using queries, the "FOR EACH" syntax may be slower than the "WHILE NOT HOut" syntax. When using FOR EACH, the context is automatically saved/restored (equivalent to HSavePosition/HRestorePosition).

Filter (syntaxes 2)

The general syntax of a filter has the following format:
"<Item name> <Operators> <Item value>"
For example:
"CustomerName > 'Doe' and ZipCode = 75 or CustomerAge >= 32"
The supported operators depend on the type of items used in the condition:
<>DifferentValid for all types
>Greater thanValid for all types
>=Greater than or equal toValid for all types
<Less thanValid for all types
<=Less than or equal toValid for all types
=Strictly equal toValid for all types
~=Almost equal toValid for the "string" types only
]ContainsValid for the "string" types only
]=Starts withValid for the "string" types only
Remarks:
  • Constant strings must be enclosed in single quotes. For example: "CustomerName = '"+Customer+"'"
  • <Item name> must only contain letters, digits and underscore characters ("_"). If <Item name> contains other characters (quote, ...), the name of the item must be enclosed in double quotes. For example: "e_mail@"]'com'
  • Comparisons between strings are performed according to the ASCII value of the characters and not according to the lexicographic value ('a' > 'Z').
  • Binary memos and composite keys cannot be part of an <Item value>.
  • If <Item value> contains a single or double quote, the single (or double) quote must be preceded by a backslash.

Exiting from FOR EACH loop

Several statements are available:
  • RETURN: Exit from the FOR EACH loop and exit from the current process (or procedure).
  • RETURN: Return a status report to the calling process. Exit from the FOR EACH loop and exit from the current process (or procedure).
  • BREAK: Exits from the FOR EACH loop and runs the rest of the current process.
Close is used to exit from the FOR EACH loop and to close the current window.
Caution: RETURN and RETURN cannot be used in the same process.

Influence of the mode for exiting from the loop on the automatic browse of data files

  • If the browse ends automatically:
    • The position in the data file before the browse is restored.
    • The possible filter required for the browse is disabled.
  • If the browse is interrupted (BREAK, RETURN, RESULT, Close, ...):
    • The position in the data file before the browse is not restored.
    • The possible filter required for the browse is not disabled. It must be disabled manually (HDeactivateFilter).
If the keyword WIHTOUTSAVEPOSITION has been used, the position save and restore operations performed during the browse are disabled. Indeed, FOR EACH automatically saves the position at the beginning of the loop and restores this position in case of "normal" exit from the loop (the position is not restored if the loop is interrupted by the keywords BREAK, RETURN or RESULT).
These position save and restore operations can have a high impact (mainly in terms of time), especially if the code using the instruction FOR EACH itself is called in a loop.

Running the next iteration

To directly run the next iteration without ending the code of the current iteration, use the Continue statement:
// Browse FileName in the order of KeyItem
FOR EACH FileName ON KeyItem
...
IF Condition = True THEN CONTINUE   // Return to the FOR EACH keyword
// and go to the next record
...
END

Modifying the key used for the automatic browse of data files or queries

If the value of the search item is modified when browsing a data file (or a query), some records may be browsed several times.
Indeed, modifying the browse item updates the file index key. This modification is taken into account during the automatic reading of the next records.
This remark is also valid for the automatic browse of a sorted query (ORDER BY) without search key.
// Browse FileName on KeyItem
FOR EACH FileName ON KeyItem
...
IF Condition = True THEN
// Modify the value of the search key
FileName.KeyItem = "New value"
HModify(FileName)
 
END
// If the new value of the search key
// positions on the record after in the browse order
// the record will be read again during the browse.
...
END
Minimum version required
  • Version 9
This page is also available for…
Comments
Error in the help
"When using queries, the "FOR EACH" syntax may be slower than the "WHILE NOT HOut" syntax. Indeed, when using FOR EACH, an operation for saving/restoring context is automatically performed (equivalent to HSavePosition/HRestorePosition). "

This text is not true at all,
On my tests using While Not Hout is much more slower than using For Each using a query

Tests:
Hout 55 seconds
For Each 24 seconds
Diego Sanchez
05 Mar. 2019