ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage syntax / Structured statements
  • Equivalence
  • Filter (syntax 2)
  • Exiting from FOR EACH loop
  • Influence of the mode for exiting from the loop on the automatic iteration of data files
  • Running the next iteration
  • Modifying the key used for the automatic iteration 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 run different types of HFSQL loops:
  • Full loop (according to a specified key or not)
  • Loop with filter (simple filter, filter on a key or filter on the search key). In this case, the filters accept the operators of HFilter.
You can loop through data files, views, queries or data sources. 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
FOR EACH Customer
// Add customers into the List Box control
ListAdd(LIST_CustomerList, Customer.CustomerNum)
END
// Syntax 1
FOR EACH Customer ON CustomerNum
// Add customers into the List Box control
ListAdd(LIST_CustomerList, Customer.CustomerNum)
END
// Syntax 2
// Loop with filter
FOR EACH Customer where "CustomerCity = 'Montpellier'"
// Add customers into the List Box control
ListAdd(LIST_CustomerList, Customer.CustomerNum)
END
 
City = "Lyon"
FOR EACH Customer where "CustomerCity = '" + City + "' and CustomerAge >= 21"
// Add customers into the List Box control
ListAdd(LIST_CustomerList, Customer.CustomerNum)
END
// Syntaxe 2
// Parcours avec filtre sur une clé spécifique
FOR EACH Client where "VilleClient = 'Montpellier'" ON NumClient
// Ajout des clients dans le champ Liste
ListAdd(LISTE_ListeClient, Client.NumClient)
END
 
Ville = "Lyon"
FOR EACH Client where "VilleClient = '" + Ville + "' ET " + ...
 "AgeClient >= 21" ON NumClient
// Ajout des clients dans le champ Liste
ListAdd(LISTE_ListeClient, Client.NumClient)
END
// Syntaxe 3
// Comparaison par rapport à une valeur
FOR EACH Client where NomClient = "Dupont"
// Ajout des clients dans le champ Liste
ListAdd(LISTE_ListeClient, Client.NumClient)
END
 
// Comparaison par rapport à une valeur
FOR EACH Commande where DateCommande = "20031231"
// Ajout des commandes dans le champ Liste
ListAdd(LISTE_ListeCommande, Commande.NumCommande)
END
// Syntaxe 3
// Comparaison par rapport à un intervalle de valeurs
FOR EACH Commande where DateCommande = "20030101" TO "20031231"
// Ajout des commandes dans le champ Liste
ListAdd(LISTE_ListeCommande, Commande.NumCommande)
END
 
// Comparaison par rapport à un intervalle de valeurs
FOR EACH Commande where "20030101" <= DateCommande <= "20031231"
// Ajout des commandes dans le champ Liste
ListAdd(LISTE_ListeCommande, Commande.NumCommande)
END
// Syntax 4
// Loop with filter on a specific key
FOR EACH Customer where CityCustomer [= "Mont" FromEnd
// Add customers into the List Box control
ListAdd(LIST_CustomerList, Customer.CustomerNum)
END
// Syntax 5
// Loop with filter on a composite key
FOR EACH ContactFile where CCState = ["Prospect", 69]
// Add contacts into the List Box control
ListAdd(LIST_ContactList, ContactFile.ContactNum)
END
 
// Loop with filter on a composite key
FOR EACH ContactFile where CZipCode [= ["Prospect", 69]
// Add contacts into the List Box control
ListAdd(LIST_ContactList, ContactFile.ContactNum)
END
Syntax

1 - Full loop Hide the details

1. Full loop using 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 loop using 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 loop through.
<ON>:
Defines the type of loop.
<Key item>:
Name of the key item used to loop through the HFSQL data file, view or query.
<WITHOUTSAVEPOSITION>:
Optional keyword.
Disables the position save and restore features in the loop. 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 iteration direction:
FromBeginning
(default value)
Loops through the data file from the first element to the last.
FromEndLoops through the data file from the last element to the first.
<END>:
Marks the end of the statement block.

2 - Loop with filter Hide the details

1. Loop 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. Loop 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 loop through.
<WHERE>:
Defines the type of loop.
<Nth condition>:
Nth condition of the filter for the HFSQL loop. For more details, see the Remarks.
<AND/OR/NOT>:
Optional logical operators used to combine the different filter conditions.
<WITHOUTSAVEPOSITION>:
Optional keyword.
Disables the position save and restore features in the loop. 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 iteration direction:
FromBeginning
(default value)
Loops through the data file from the first element to the last.
FromEndLoops through the data file from the last element to the first.
<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 - Loop 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 loop through.
<WHERE>:
Defines the type of loop.
<Key item>:
Key item of the data file to loop through.
<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 features in the loop. 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 iteration direction:
FromBeginning
(default value)
Loops through the data file from the first element to the last.
FromEndLoops through the data file from the last element to the first.
<END>:
Marks the end of the statement block.

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

FOR EACH <File> WHERE <Key item> [= <Beginning of search value> [[WITHOUTSAVEPOSITION,] [<Direction>]]
...
END
<FOR EACH>:
Marks the beginning of the statement block.
<File>:
Name of the HFSQL data file, view or query to loop through.
<WHERE>:
Defines the type of loop.
<Key item>:
Key item of the data file to loop through.
<Beginning of search value>:
Comparison value of the key item.
<WITHOUTSAVEPOSITION>:
Optional keyword.
Disables the position save and restore features in the loop. 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 iteration direction:
FromBeginning
(default value)
Loops through the data file from the first element to the last.
FromEndLoops through the data file from the last element to the first.
<END>:
Marks the end of the statement block.

5 - Loop 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 loop through.
<WHERE>:
Defines the type of loop.
<Composite key>:
Composite key of the data file to loop through.
<Value of component N>:
Value of the component.
<WITHOUTSAVEPOSITION>:
Optional keyword.
Disables the position save and restore features in the loop. 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 iteration direction:
FromBeginning
(default value)
Loops through the data file from the first element to the last.
FromEndLoops through the data file from the last element to the first.
<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 loop uses the filters previously defined on the data file.
    remark: Crossed-out records are taken into account in the loop.
  • The syntax FOR EACH <File> WHERE <Condition> is used to define a filter and to loop through the data file. This loop ignores the filters that were previously defined.
  • 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 (syntax 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 iteration of data files

  • If the loop ends automatically:
    • The position in the data file before the start of the loop is restored.
    • Any filter required for the loop is disabled.
  • If the loop is interrupted (BREAK, RETURN, RESULT, Close, etc.):
    • The position in the data file before the start of the loop is not restored.
    • Any filter required for the loop remains enabled. It must be disabled manually (HDeactivateFilter).
If the keyword WIHTOUTSAVEPOSITION has been used, the position save and restore operations performed in the loop 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:
// Loop through 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 iteration of data files or queries

If the value of the item is modified when looping through a data file (or a query), some records may be looped through several times.
Changing the browse item updates the file index key. This modification is taken into account during the automatic reading of the next records.
This is also valid for an automatic loop of a sorted query (ORDER BY) without search key.
// Loop through FileName using 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
// sets the position on the following record in the iteration order
// the record will be read again during the loop.
...
END
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 09/05/2023

Send a report | Local help