4GL ABL Openedge loop through handle? - progress-4gl

here is my current code
def var hbTT as handle.
for each Cust:
hbTT:buffer-create().
assign
hbTT::Name = Cust.Name
hbTT::address = Cust.Address.
end.
now what I want to do is to loop through hbtt. How can I do that?
I tried
for each hbTT:
/* Do something */
end.
the error I get is
unknown or ambiguous table hbTT. (725)
thank you

You won't be able to do a loop that way, as for each requires a static name.
Instead, try this:
DEFINE VARIABLE hQuery AS HANDLE NO-UNDO.
create query hQuery.
hQuery:set-buffers(hbtt).
hquery:query-prepare('for each tt'). /* <-- Where tt is the original buffer name */
hquery:query-open().
hquery:get-first().
do while not hquery:query-off-end:
disp hbtt::name hbtt::address .
hquery:get-next().
end.

Related

How to execute procedure from List with parameters in Progress 4gl?

I have some list like this
DEFINE VARIABLE procedurelist AS CHARACTER EXTENT 5
INITIAL [ "1.p", "2.p", "3.p", "4.p", "5.p"].
but this all procedures with input-output parameters and i want to execute this procedure, How can i do this? I have no idea how to do this.
The base of your solution is the RUN VALUE statement.
The manual states.
VALUE( extern-expression ) An expression that returns the name of the (local or remote) external procedure you want to run....
This basically means that you can input a string with the value of a program (or procedure) into your RUN statement.
If all input-output parameters are exactly the same you can do like this:
DEFINE VARIABLE procedurelist AS CHARACTER EXTENT 5 INITIAL [ "1.p", "2.p", "3.p", "4.p", "5.p"].
DEFINE VARIABLE iExtent AS INTEGER NO-UNDO.
DEFINE VARIABLE cVariable AS CHARACTER NO-UNDO.
DO iExtent = 1 TO EXTENT(procedurelist):
RUN VALUE(procedurelist[iExtent]) (INPUT-OUTPUT cVariable).
END.
If the parameters are different it gets trickier (but not impossible). The CREATE CALL and the Call Object can help you there. In this case you would need some kind of way to keep track of the different parameters as well.
Here's a basic example taken directly from the online help:
DEFINE VARIABLE hCall AS HANDLE NO-UNDO.
CREATE CALL hCall.
/* Invoke hello.p non-persistently */
hCall:CALL-NAME = "hello.p".
/* Sets CALL-TYPE to the default */
hCall:CALL-TYPE = PROCEDURE-CALL-TYPE.
hCall:NUM-PARAMETERS = 1.
hCall:SET-PARAMETER(1, "CHARACTER", "INPUT", "HELLO WORLD").
hCall:INVOKE.
/* Clean up */
DELETE OBJECT hCall.

OpenEdge Dynamic TempTable

I am creating an application that accesses a database with many tables.
To make the coding easier and shorter, I am planning to make one procedure that either gets/sets the data dynamically or executes a procedure for specific data manipulations.
I've got something so far, but I'm kind of stuck now.
What I've done so far is made sure I can dynamically built a temp-table with the same schema as a database table I want to retrieve data from. I then query for a record and add it to the dynamic temp-table. Then this temp-table is passed as an output parameter.
What I want to do now is , when the user has changed the record, save that record dynamically. Therefore, I have to query the table dynamically and find the record the user wants to change. Actually the same as retrieving the record. But saving the changes made requires me to go through the input dynamic temp-table. How is this done?
The normal way of actions is like this:
- Get record by passing the table name and key. Then give the record to output parameter.
- Update record by getting dynamic temp-table as input parameter and then saving the changes from the correct record to the correct record. This second part is where I fail.
The code supplied here only does the first part, but the second part should be included into this code.
Code:
DEF VAR G-TableBuf AS HANDLE NO-UNDO.
DEF VAR G-TableBuf-Handle AS HANDLE NO-UNDO.
DEF VAR G-Query AS HANDLE NO-UNDO.
DEF VAR G-Table-FirNr AS INT NO-UNDO.
DEF VAR G-Qstring AS CHAR NO-UNDO.
DEF VAR G-Heeft-FirNr AS LOG NO-UNDO.
DEF VAR G-TempTable AS HANDLE NO-UNDO.
DEF VAR G-tt-Buffer AS HANDLE NO-UNDO.
DEF VAR G-MatchZone AS CHAR NO-UNDO.
DEF VAR G-prime-field AS CHAR NO-UNDO.
DEF VAR G-Zones-Buffer AS HANDLE NO-UNDO.
{lib/def_tt_ds_Errors.i}
DEF INPUT PARAMETER p_iFirnr AS INT NO-UNDO.
DEF INPUT PARAMETER p_iApplNr AS INT NO-UNDO.
DEF INPUT PARAMETER p_cUsrCd AS CHAR NO-UNDO.
DEF INPUT PARAMETER p_cAction AS CHAR NO-UNDO.
DEF INPUT PARAMETER p_cKeyCd AS CHAR NO-UNDO. /* Record key */
DEF INPUT PARAMETER p_cTable AS CHAR NO-UNDO. /* Table name */
DEF INPUT-OUTPUT PARAMETER TABLE-HANDLE hTT. /* INPUT-OUTPUT dynamic temp-table */
DEF OUTPUT PARAMETER DATASET FOR dsErrors.
RUN FindRecord.
RETURN.
PROCEDURE FindRecord :
/*------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------*/
DEF VAR i AS INT NO-UNDO.
CREATE TEMP-TABLE G-TempTable.
CREATE BUFFER G-TableBuf FOR TABLE p_cTable.
CREATE QUERY G-Query.
ASSIGN G-Table-FirNr = Get-Fir (p_cTable)
G-MatchZone = "kolom,Waarde"
G-heeft-firnr = FALSE
G-Zones-Buffer = BUFFER zones:HANDLE
G-TableBuf-Handle = G-TableBuf:HANDLE.
/* SCHEMA BUILDING CODE GOES HERE */
G-TempTable:TEMP-TABLE-PREPARE("tt" + p_cTable).
G-tt-Buffer = G-TempTable:DEFAULT-BUFFER-HANDLE.
G-tt-Buffer:EMPTY-TEMP-TABLE().
hTT = G-TempTable.
G-Qstring = "FOR EACH " + p_cTable.
G-Query:SET-BUFFERS(G-TableBuf).
G-Query:QUERY-PREPARE(G-Qstring).
G-Query:QUERY-OPEN().
G-Query:GET-NEXT().
REPEAT:
IF G-query:QUERY-OFF-END THEN
LEAVE.
G-tt-Buffer:BUFFER-CREATE.
G-tt-Buffer:BUFFER-COPY (G-TableBuf-Handle).
G-Query:GET-NEXT().
END.
END PROCEDURE.
Thanks in advance!
You need to study the ProDataset functionality, it'll make loading a TT and saving it's changed records back to the DB a lot easier.
To create a dynamic TT like a db table (at a high level), get the table's buffer handle, then go through it using b-handle:buffer-field(field-number):name to get a list of fields, and then use add-fields-from to create fields in the TT based on the db table.
Once you've got that, use the Prodataset "FILL" functionality to load data into it, and then pass the PDS back to the calling program for it to use.
when you're ready to save the data out, use the PDS functionity (save-row-changes) to save the changed records back to the database.
The process of loading and saving TT records is largely documented in the Prodatset set of docs. I highly recommend the 11.3 docs, as they're much improved over the prior version PDS docs.
This KB will also give you some ideas on how to build a dynamic TT like a db table. http://knowledgebase.progress.com/articles/Article/000045189?q=how+to+create+dynamic+temp-table&l=en_US&c=Product_Group%3AOpenEdge&type=Article__kav&fs=Search&pn=1
I used 2 different parameters for the dynamic temp-table, one for input and one for output. The rest I figured out myself. Thanks for your help guys!

How find an object using Progress 4GL?

I need to find an object in a progress session... I don't know how to do... only with a sequential search, but it's very expensive(time consuming) if the number of objects is relative big.
Is there another way to do this?
define variable myObject As character no-undo.
define variable loop as Progress.Lang.Object no-undo.
assign myObject = "1234".
loop = Session:First-object.
do While valid-object(loop) :
if (loop:tostring() = myObject) then Do:
MESSAGE "Found!!!"
VIEW-AS ALERT-BOX INFO BUTTONS OK.
leave.
end.
loop = loop:Next-sibling.
end.
Thank you.

Bracketed key value - limit OR foreach ... in OR?

Continuing my quest to convert .NET to Progress, I faced another challenge yesterday.
Our company bought time ago a .NET DLL to manage Excel document without the need to install Microsoft Excel. There is several functions that return a series of cells depending of the need.
The returned value is a class that implement IEnumerator interface in .NET.
The problem is that I cannot find a way to iterate trough the cells without getting the error:
System.ArgumentException: Row or column index is invalid or out of required range
Is there a way to in Progress to validate if X is inside of the extent range?
OR
Is there a way to iterate trough the array without knowing the upper limit of the array?
Thank you!
Sebastien
--- temporary solution ---
/* declaration */
DEFINE VARIABLE oCell AS CLASS GemBox.Spreadsheet.ExcelCell NO-UNDO.
DEFINE VARIABLE oRange AS CLASS GemBox.Spreadsheet.CellRange NO-UNDO.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
/* load excel file */
...
/* retrieve a series of cells */
ASSIGN oRange = oWorksheet:Cells:GetSubrangeAbsolute(1,1, 2,2).
/* first cell */
ASSIGN i = 0.
ASSIGN oCell = ?.
ASSIGN oCell = oRange:Item[i] NO-ERROR.
/* validate cell is in the range */
DO WHILE NOT oCell EQ ?:
MESSAGE oCell:Value VIEW-AS ALERT-BOX.
/* next cell */
ASSIGN i = i + 1.
ASSIGN oCell = ?.
ASSIGN oCell = oRange:Item[i] NO-ERROR.
END.
I don't have access nor I can test this solution, but if it implements correctly the interface some solution like this one should work:
/* declaration */
DEFINE VARIABLE oCell AS CLASS GemBox.Spreadsheet.ExcelCell NO-UNDO.
DEFINE VARIABLE oRange AS CLASS GemBox.Spreadsheet.CellRange NO-UNDO.
DEFINE VARIABLE oEnumerator AS CLASS System.Collections.IEnumerator NO-UNDO.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
/* load excel file */
...
/* retrieve a series of cells */
ASSIGN oRange = oWorksheet:Cells:GetSubrangeAbsolute(1,1, 2,2).
oEnumerator = oRange:getEnumerator().
DO WHILE oEnumerator:MoveNext():
oCell = CAST(oEnumerator:current,"GemBox.Spreadsheet.ExcelCell").
END.
If it doesn't work exactly like this, at least it should point you in the correct direction to use it.
From the web page I'd infer that the # of cols =
oRange:LastColumnIndex - oRange:FirstColumnIndex
and the # of rows is
oRange:LastRowIndex - oRange:FirstRowIndex
I'd think using
oCell = oRange:Item[Int32, Int32]
to get the item at the row, col position would work better instead of using a single element array element.

ABL inserting and displaying table data

I apologies for this being a very simple question but as a first time user of ABL open edge and im stuck. I have enter values into a table like so
METHOD PRIVATE VOID POPULATETABLE ( ):
DEFINE VARIABLE I AS INTEGER.
DO I = 0 TO 100:
CREATE TEST.
ASSIGN TEST.CUSTOMER_NAME="SMITH"
TEST.ORDER_NUMBER=I
TEST.ORDER="BOOKS"
TEST.COST=45.00
TEST.CUSTOMER_NAME = "JACKSON"
TEST.ORDER_NUMBER=I
TEST.ORDER="PAPER CLIPS"
TEST.COST=1.7.
ASSIGN TEST.CUSTOMER_NAME="JONES"
TEST.ORDER_NUMBER =I
TEST.ORDER="PENCILS"
TEST.COST=2.50
TEST.CUSTOMER_NAME = "TURNER"
TEST.ORDER_NUMBER = I
TEST.ORDER="PENS"
TEST.COST=0.7.
END.
END METHOD.
and I'm trying to display them using this
FOR EACH TEST:
DISPLAY TEST.COST TEST.CUSTOMER_NAME TEST.ORDER TEST.ORDER_NUMBER.
RETURN.
END.
However the result only shows the last row of data entered. can anyone help, I'm even unsure on whether the display function is right or the assign is.
The "return" in your FOR EACH is causing the code to leave the loop after the first record. Delete that statement and you'll see all the records.
FOR EACH TEST:
DISPLAY TEST.COST
TEST.CUSTOMER_NAME
TEST.ORDER
TEST.ORDER_NUMBER.
RETURN. /* this is why you're only seeing one record - */
/* get rid of this and you'll see all the records */
END.
I would avoid assigning an order# of 0. It's just asking to confuse people.
define variable i as integer no-undo.
do i = 1 to 100:
create test.
assign
test.order_number = i
test.customer = "smith" /* you need some way to get */
test.order = "books" /* actual data for the rest */
test.cost = random( 10, 100) /* of the fields... */
.
end.
And then review the orders with:
for each test no-lock:
display test.
end.
Yeah, all I needed was a create statement per each assign for each record and that worked. Thanks everyone, the working coded looks like:
CREATE TEST.
ASSIGN TEST.CUSTOMER_NAME="SMITH"
TEST.ORDER_NUMBER=I
TEST.ORDER="BOOKS"
TEST.COST=45.00.
CREATE TEST.
ASSIGN TEST.CUSTOMER_NAME = "TAYLOR"
TEST.ORDER_NUMBER=I
TEST.ORDER="PAPER CLIPS"
TEST.COST=1.7.
CREATE TEST.
ASSIGN TEST.CUSTOMER_NAME="THOMPSON"
TEST.ORDER_NUMBER =I
TEST.ORDER="PENCILS"
TEST.COST=2.50.
CREATE TEST.
ASSIGN TEST.CUSTOMER_NAME = "TURNER"
TEST.ORDER_NUMBER = 2
TEST.ORDER="PENS"
TEST.COST=0.7.
FOR EACH TEST WHERE TEST.COST > 1.3 BY TEST.ORDER_NUMBER:
DISPLAY TEST.
END.