Using variable in FOR EACH

carel

New Member
Hi,
is possible use table name in FOR EACH statement as variable?

For example something like following:
DEFINE VARIABLE tbl AS CHARACTER.
tbl = "table1" (or get it by PROMPT-FOR)
FOR EACH tbl ...

Thanks,
Carel
 
Hi,
is possible use table name in FOR EACH statement as variable?

For example something like following:
DEFINE VARIABLE tbl AS CHARACTER.
tbl = "table1" (or get it by PROMPT-FOR)
FOR EACH tbl ...

it's possible but in a bit different way and if your progress version is 9 or higher

Code:
DEFINE VARIABLE tbl AS CHARACTER.
tbl = "table1" (or get it by PROMPT-FOR)
DEFINE VARIABLE tblbuff AS HANDLE     NO-UNDO.
CREATE BUFFER tblbuff FOR TABLE tbl.
DEFINE VARIABLE q AS HANDLE     NO-UNDO.
CREATE QUERY q.
q:SET-BUFFERS(tblbuff).
q:QUERY-PREPARE("FOR EACH " + tbl ...).
q:QUERY-OPEN().
q:GET-FIRST().
DO WHILE tblbuff:AVAIL:
   /* your actions should be here */
   q:GET-NEXT().
END.
DELETE OBJECT q.
DELETE OBJECT tblbuff.
 
If it is a simple FOR EACH statement then you have a number of options.
1) FOR EACH {&tableName} , use a preprocessor and change the tablename on-the-fly

2) Clause around the statement eg.. Use a CASE

However slightly different with a query. Though with a query you require a number of data accesses that will be envoked likely to use option 2
 
If it is a simple FOR EACH statement then you have a number of options.
1) FOR EACH {&tableName} , use a preprocessor and change the tablename on-the-fly

It seems like an exciting opportunity because i have no idea how to assign a runtime variable value to a preprocessor value?! ;) Could you provide us with details?
 
FIND tableName WHERE fieldName = {&preField} NO-LOCK.

Earlier in your code block define {&preField} value ie on-the-fly.
Something like;

&SCOPED-DEFINE preField pantsString



Hope you can work out from this how to apply.
 
FIND tableName WHERE fieldName = {&preField} NO-LOCK.

Earlier in your code block define {&preField} value ie on-the-fly.
Something like;

&SCOPED-DEFINE preField pantsString



Hope you can work out from this how to apply.

Oops, your sample is ok but we're talking about tablename kept in the runtime variable. IOW you can't do that

Code:
def var pantsString as char no-undo.
&SCOPED-DEFINE preTable pantsString
 
FIND {&preTable} NO-LOCK.
 
Oops sorry, yeah not at my desk.
IF you define your FOR EACH table WHERE field multiple times, then dynamically call the desired criteria. For which you can have the data stuffed in a preprocessor.

Hope you can get a handle on my methods.
 
Back
Top