Referencing a database field via an alias

jmac12

Member
Hi I’m using open edge 10.2b I'm wondering if it’s possible to reference a database field via a alias. In my example im outputting to excel
I’ve got the temp table name in a alias &scoped-define browse-table ttRecs im looping round the columns on browse to get my data but I cant dynamically say which field I want so I have to do lines and lines of code. Is there any way to make this more generic I know what the field im looking for, just don’t know how to do it via say a char. Any ideas would be great? i.e. a pointer or something?


get first {&browse-name}.
repeat:

process events.
if not available {&browse-table} then
leave.

assign chrLine = ""
intRow = intRow + 1.

do intI = 1 to num-entries(xHeaders):

assign chrVariable = ""
hName = entry(intI,xHeaders).

case hName:
when "grade-code" then assign chrVariable = string({&browse-table}.grade-code) /*want the grade code bit to be in alias or pointer!!*/
 
You should use dynamic queries.

example:
export any tables or temp-table to cvs format.
tab2cvs("_File").

Code:
FUNCTION tab2csv RETURNS CHARACTER (INPUT mytab as character).

CREATE BUFFER hBuf FOR TABLE mytab.
CREATE QUERY qBuf.
qBuf:SET-BUFFERS(hBuf).
qBuf:QUERY-PREPARE("FOR EACH " + mytab + " NO-LOCK").
qBuf:QUERY-OPEN().
qBuf:GET-FIRST(). stmp = "".
 REPEAT i = 1 TO hBuf:NUM-FIELDS:
                hDBFld = hBuf:BUFFER-FIELD (i).
                tmp = STRING (hDBFld:LABEL).
                if i = 1 then stmp = tmp.
               else stmp = stmp + ";" + tmp.
END.
PUT UNFORMATTED stmp SKIP.
qBuf:SET-BUFFERS(hBuf).
qBuf:QUERY-PREPARE("FOR EACH " + mytab + " NO-LOCK").
qBuf:QUERY-OPEN().
qBuf:GET-FIRST().  
  REPEAT:
    IF NOT hBuf:AVAILABLE then LEAVE.
    stmp = "".
              REPEAT i = 1 TO hBuf:NUM-FIELDS:
                hDBFld = hBuf:BUFFER-FIELD (i). 

                 CASE hDBFld:data-type:
                    WHEN "integer"   THEN do: sType = "Number".    end.
                    WHEN "decimal"   THEN do: sType = "Number".    end.
                    WHEN "character" THEN do: sType = "String".    end.
                    WHEN "date"      THEN do: sType = "DateTime".  end. 
                    WHEN "logical"   THEN do: sType = "String".    end. 
                    OTHERWISE             do: sType = "String".    end.
                 END CASE.

                 if hDBFld:data-type <> "character" then tmp = STRING ( hDBFld:BUFFER-VALUE ).
                                                    else tmp = hDBFld:BUFFER-VALUE.
                     
                 if sType = "Boolean"  then if tmp = "yes" then tmp = "1". 
                                                           else tmp = "0".
                                                           
                 if sType = "DateTime" then tmp = String(Date(tmp)).
                 if hDBFld:BUFFER-VALUE = ? then tmp = "".
                 tmp = REPLACE( tmp, ";" , " ").
                 tmp = REPLACE( tmp, "\r", " ").
                 tmp = REPLACE( tmp, "\n", " ").
                 if i = 1 then stmp = tmp.
                          else stmp = stmp + ";" + tmp.
              END.

    PUT UNFORMATTED stmp skip.
    qBuf:GET-NEXT().
    IF qBuf:QUERY-OFF-END THEN LEAVE.
  END.

qBuf:QUERY-CLOSE().
output close.
 
cheers mate I didnt need all of that what i did in the end was:

DEFINE VAR bh AS WIDGET-HANDLE.
DEFINE VAR hDBFld AS WIDGET-HANDLE.

bh = buffer ttRecs:handle.

/*get the field i want then output the value*/
do intI = 1 to num-entries(xHeaders):

assign chrVariable = ""
hName = entry(intI,xHeaders).


hDBFld = bh:BUFFER-FIELD (hName).
chrVariable = hDBFld:buffer-value.

it means this way i can select the field i want then output it. thanks for you're help this has saved me lots of time
 
If i remeber right you have to use
bh = buffer ttRecs:handle. //for DB tables, static temp-tables
and
bh = buffer ttRecs:default-buffer-handle. // for dynamic temp-tables
 
Back
Top