PC SOFT

ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Home | Sign in | English EN
  • 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.
Versions 17 and later
iPhone/iPad This feature is available for iPhone/iPad applications.
New in version 17
iPhone/iPad This feature is available for iPhone/iPad applications.
iPhone/iPad This feature is available for iPhone/iPad applications.
Versions 18 and later
Universal Windows 10 App This feature is available in Windows Store apps mode.
Android Widget This feature is available in Android widget mode.
New in version 18
Universal Windows 10 App This feature is available in Windows Store apps mode.
Android Widget This feature is available in Android widget mode.
Universal Windows 10 App This feature is available in Windows Store apps mode.
Android Widget This feature is available in Android widget mode.
Versions 21 and later
Universal Windows 10 App This feature is available in Universal Windows 10 App mode.
Apple Watch This feature is available in Apple Watch mode.
New in version 21
Universal Windows 10 App This feature is available in Universal Windows 10 App mode.
Apple Watch This feature is available in Apple Watch mode.
Universal Windows 10 App This feature is available in Universal Windows 10 App mode.
Apple Watch This feature is available in Apple Watch mode.
Remark: From version 19, HFSQL is the new name of HyperFileSQL.
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
// Browse 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
// Syntax 2
// Browse with filter on a specific key
FOR EACH Customer where "CustomerCity = 'Montpellier'" ON CustomerNum
// Add customers into the List Box control
ListAdd(LIST_CustomerList, Customer.CustomerNum)
END
 
City = "Lyon"
FOR EACH Customer where "CustomerCity = '" + City + "' AND " + ...
 "CustomerAge >= 21" ON CustomerNum
// Add customers into the List Box control
ListAdd(LIST_CustomerList, Customer.CustomerNum)
END
// Syntax 3
// Comparison according to a value
FOR EACH Customer where CustomerName = "Doe"
// Add customers into the List Box control
ListAdd(LIST_CustomerList, Customer.CustomerNum)
END
 
// Comparison according to a value
FOR EACH Orders where OrderDate = "20031231"
// Add orders into the List Box control
ListAdd(LIST_OrderList, Order.OrderNum)
END
// Syntax 3
// Comparison according to a set of values
FOR EACH Orders where OrderDate = "20030101" TO "20031231"
// Add orders into the List Box control
ListAdd(LIST_OrderList, Order.OrderNum)
END
 
// Comparison according to a set of values
FOR EACH Orders where "20030101" <= OrderDate <= "20031231"
// Add orders into the List Box control
ListAdd(LIST_OrderList, Order.OrderNum)
END
// Syntax 4
// Browse 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
// Browse 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
 
// Browse 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 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>:
Versions 24 and later
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.
New in version 24
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.
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>:
Versions 24 and later
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.
New in version 24
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.
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>:
Versions 24 and later
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.
New in version 24
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.
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>:
Versions 24 and later
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.
New in version 24
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.
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>:
Versions 24 and later
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.
New in version 24
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.
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).
Versions 24 and later
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.
New in version 24
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.
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
Click [Add] to post a comment