Help with extracting field names from a table

stuartb42

New Member
Hello,

Was wondering if anybody could help. Bit new at progress code.
I have written code to export data out of progress into a txt file. Which works fine.
I am now trying to find a way of exporting Field Names from a table.

The code below I have used to export the data. What I would like to do is be able to create the header line cTemp automatically as some of the tables are rather large.

Any help would be much appreciated....

DEFINE VARIABLE cTemp AS CHAR NO-UNDO.
DEFINE STREAM OutData.
OUTPUT STREAM OutData TO VALUE("IH.RM_TRADES.txt").

/* header line */
ASSIGN cTemp = "ORG-CODE|TRADE-CODE|DESCRIPTION"
cTemp = cTemp + "|~n".
/* write header line to file */
PUT STREAM OutData UNFORMATTED cTemp.

/* main loop */
FOR EACH IH.RM_TRADES:
ASSIGN cTemp = STRING(ORG-CODE) + "|" + STRING(TRADE-CODE) + "|" + STRING(DESCRIPTION)
cTemp = cTemp + "|~n".
/* write line to file */
PUT STREAM OutData UNFORMATTED cTemp.
END.

OUTPUT STREAM OutData CLOSE.


Stuart
 

bulklodd

Member
The code below I have used to export the data. What I would like to do is be able to create the header line cTemp automatically as some of the tables are rather large.

You need to write dynamic export procedure. Here it is

Code:
DEFINE VARIABLE cTemp AS CHAR NO-UNDO.
DEFINE STREAM OutData.
DEFINE VARIABLE i AS INTEGER    NO-UNDO.
DEFINE VARIABLE vBuffer AS HANDLE     NO-UNDO.
OUTPUT STREAM OutData TO VALUE("IH.RM_TRADES.txt").
vBuffer = BUFFER IH.RM_TRADES:HANDLE.
DO i = 1 TO vBuffer:NUM-FIELDS:
   cTemp = cTemp + (IF cTemp = "" THEN "" ELSE "|") +
      vBuffer:BUFFER-FIELD(i):NAME.
END.
/* write header line to file */ 
PUT STREAM OutData UNFORMATTED cTemp SKIP. 
FOR EACH IH.RM_TRADES:
   DO i = 1 TO vBuffer:NUM-FIELDS:
      cTemp = cTemp + (IF cTemp = "" THEN "" ELSE "|") +
         vBuffer:BUFFER-FIELD(i):BUFFER-VALUE.
   END.
   /* write line to file */ 
   PUT STREAM OutData UNFORMATTED cTemp SKIP. 
END.
OUTPUT STREAM OutData CLOSE.

Although it needs a bit of improvements nevertheless it's okay.

HTH
 

joey.jeremiah

ProgressTalk Moderator
Staff member
another option

Code:
/* define query, fields, sort order etc. to export */

define query qryItem

    for item fields ( itemnum itemname weight ).

open query qryItem

    for each item no-lock by itemname.



/* export query */

output to item.csv. /* using default output stream */

    run exportQuery( query qryItem:handle ).

output close.



procedure exportQuery:

    define input param phQuery as handle no-undo.



    /* fetch query ( avail ) fields */

    define var iBuffer  as int no-undo.
    define var hBuffer  as handle no-undo.

    define var iField   as int no-undo.
    define var hField   as handle no-undo.
    define var cFields  as char no-undo.

    phQuery:get-first( ).

    do iBuffer = 1 to phQuery:num-buffers:
       hBuffer = phQuery:get-buffer-handle( iBuffer ).

        do iField = 1 to hBuffer:num-fields:
           hField = hBuffer:buffer-field( iField ).

            if hField:available then
            cFields = cFields + 

                ( if cFields ne "" then "," else "" ) +
                string( hField ).
            
        end. /* iField */

    end. /* iBuffer */



    /* export header */

    do iField = 1 to num-entries( cFields ):
       hField = widget-handle( entry( iField, cFields ) ).

        put unformatted quoter( hField:column-label ).

        if iField = num-entries( cFields ) then
            put skip.
        else put ",".      

    end. /* iField */



    /* export data */

    phQuery:get-first( ).

    repeat while not phQuery:query-off-end:

        do iField = 1 to num-entries( cFields ):
           hField = widget-handle( entry( iField, cFields ) ).

            if hField:data-type = "character" then
                 put unformatted quoter( hField:buffer-value ).
            else put unformatted hField:buffer-value.

            if iField = num-entries( cFields ) then
                put skip.
            else put ",".      

        end. /* iField */

        phQuery:get-next( ).

    end. /* repeat */

end procedure. /* exportQuery */

in most cases theres no need for all that, schema tables or just a simple display and column-labels will do the job

when it comes to code i prefer things to be neat and simple. hth
 

joey.jeremiah

ProgressTalk Moderator
Staff member
guys, anyone used the temp-table expositor on peg ?

don't know if its already been done ? but it's time we added office xml schemas and opendocument support


i played with pivot tables ( in excel ) recently to save me the work writing diff sorts, matrices, summary/detail ( using drill up/down ) etc.

i was really happy with how it came out. simple, fast you can create them on a unix machine not just windows, after all it's just text

theres so much more document types and documented and open standard to choose from nowadays. i've also played with svg, amazing!
 

stuartb42

New Member
thanks

Thanks bulklodd, that was just what I needed. Worked a treat, will save me lots of time. Much appreciated....

Stuart
 

stuartb42

New Member
run into a bit of a problem

I put this code in so I can just extract the header files.
Run into a problem with a few multi separated fields ie.

bath-rooms[1]
bath-rooms[2]
bath-rooms[3]
bath-rooms[4]
bath-rooms[5]

only gets extracted as bath-rooms

Hope that makes sense. Here is the code I use. Any idea's how I can get it to split these fields and output them like above???

DEFINE VARIABLE cTemp AS CHAR NO-UNDO.
DEFINE STREAM OutData.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE vBuffer AS HANDLE NO-UNDO.
OUTPUT STREAM OutData TO VALUE("test.txt").
vBuffer = BUFFER IH.RM_TRADES:HANDLE.
DO i = 1 TO vBuffer:NUM-FIELDS:
cTemp = cTemp + (IF cTemp = "" THEN "" ELSE "#") +
vBuffer:BUFFER-FIELD(i):NAME.
END.
/* write header line to file */
PUT STREAM OutData UNFORMATTED cTemp SKIP.
OUTPUT STREAM OutData CLOSE.
 

bulklodd

Member
stuartb42 said:
Hope that makes sense. Here is the code I use. Any idea's how I can get it to split these fields and output them like above???

Try so

Code:
DO i = 1 TO vBuffer:NUM-FIELDS:
   IF vBuffer:BUFFER-FIELD(i):EXTENT = 0 THEN
      cTemp = cTemp + (IF cTemp = "" THEN "" ELSE "#") +
         vBuffer:BUFFER-FIELD(i):NAME.
   ELSE
   DO j = 1 TO vBuffer:BUFFER-FIELD(i):EXTENT:
      cTemp = cTemp + (IF cTemp = "" THEN "" ELSE "#") +
         vBuffer:BUFFER-FIELD(i):NAME + "[" + STRING(j) + "]".
   END.
END.
As I've told you that code needs some improvements :)
 
Top