/* help-br.i 

   07/09/96 Dmitri Levin

    usage: {help-br.i
            &TABLE=GLBANK                   Table name
            &FIELD=BANK-NUM                 Field to display
            &TYPE=character                 Type of this field
            &LABEL="label ""Bank No"""      Label, if necessary to change it
            &FIELD1=BANK-NAME               An other Field, etc.
            &ASSGN=BANK-NUM                 Field you want to assign to &OUT
            &INIT="input V-BANK"            Initial = "input" and var in update
            &OUT=V-BANK}                    Var in update
*/

def var VAR-{&TABLE} like {&TABLE}.{&FIELD} no-undo.
define query {&TABLE}-QUERY for {&TABLE}.
define browse {&TABLE}-BROWSE query {&TABLE}-QUERY 
    display 
        {&TABLE}.{&FIELD}  {&LABEL}
    &if "{&FIELD1}" <> "" &then
        {&TABLE}.{&FIELD1} {&LABEL1}
    &endif
    &if "{&FIELD2}" <> "" &then
         {&TABLE}.{&FIELD2} {&LABEL2}
    &endif
    &if "{&FIELD3}" <> "" &then
        {&TABLE}.{&FIELD3} {&LABEL3}
    &endif
    &if "{&FIELD4}" <> "" &then
        {&TABLE}.{&FIELD4} {&LABEL4}
    &endif
    &if "{&FIELD5}" <> "" &then
        {&TABLE}.{&FIELD5} {&LABEL5}
    &endif
    with 10 down title "Choose".
form 
    {&TABLE}-BROWSE skip      
    with frame {&TABLE}-FRAME row 3 centered no-box.
form
    VAR-{&TABLE}
    with frame {&TABLE}-FRAME1 row 19 no-box side-labels.
form
    {&TABLE}.{&FIELD}
    with frame DUMMY-FRAME.
 
on "GO" of {&TABLE}-BROWSE do:
    apply "RETURN" to self.
end.
on "RETURN" of {&TABLE}-BROWSE do:
   {&OUT} = {&TABLE}.{&ASSGN}.                 
end.

on any-printable of {&TABLE}-BROWSE do:
    if last-event:function = "?" then do:
        update VAR-{&TABLE} with frame {&TABLE}-FRAME1.        
        hide frame {&TABLE}-FRAME1.
        &if "{&WHERE}" = "" &then
            find first {&TABLE} where
                {&TABLE}.{&FIELD} ge VAR-{&TABLE}
                no-lock no-wait no-error.
        &else
            find first {&TABLE} where {&WHERE} and
                {&TABLE}.{&FIELD} ge VAR-{&TABLE}
                no-lock no-wait no-error.
        &endif
    end. /* ? */
    else do:
    &if "{&TYPE}" begins "c" &then
        if (last-event:function ge "a" and
           last-event:function le "z") or
           (last-event:function ge "0" and
           last-event:function le "9") 
        then do:
            &if "{&WHERE}" = "" &then
                find first {&TABLE} where
                    {&TABLE}.{&FIELD} ge LAST-EVENT:FUNCTION 
                    no-lock no-wait no-error.
            &else
                find first {&TABLE} where {&WHERE} and
                    {&TABLE}.{&FIELD} ge LAST-EVENT:FUNCTION 
                    no-lock no-wait no-error.
            &endif
        end. /* a-z 0-9 */
    &endif
    end.
    if avail {&TABLE} then do:
        reposition {&TABLE}-QUERY to recid recid({&TABLE}).
    end.
    message "press ? to jump to {&FIELD} starting with input string".
end. /* on any-printable */

on "END-ERROR" of {&TABLE}-BROWSE do:
    {&OUT} = ?.
end.

&if "{&WHERE}" = "" &then
    open query {&TABLE}-QUERY for each {&TABLE} by {&TABLE}.{&FIELD} indexed-reposition.
&else
    open query {&TABLE}-QUERY for each {&TABLE} 
        where {&WHERE} by {&TABLE}.{&FIELD} indexed-reposition.
&endif

enable all with frame {&TABLE}-FRAME.

if "{&FIELD}" = "{&ASSGN}" then do:
    &if "{&TYPE}" begins "c" &then
        &if "{&WHERE}" = "" &then
            find first {&TABLE} where
                {&TABLE}.{&FIELD} begins {&INIT} no-lock no-wait no-error.
        &else
            find first {&TABLE} where {&WHERE} and
                {&TABLE}.{&FIELD} begins {&INIT} no-lock no-wait no-error.
        &endif
    &endif
    if avail {&TABLE} then 
        reposition {&TABLE}-QUERY to recid recid({&TABLE}).
    else
        reposition {&TABLE}-QUERY forwards -1. /* to initiate first record */
end.
else 
    reposition {&TABLE}-QUERY forwards -1. /* to initiate first record */

message "press ? to jump to {&FIELD} starting with input string".

wait-for "RETURN","END-ERROR" of {&TABLE}-BROWSE.
close query {&TABLE}-QUERY.
hide frame {&TABLE}-FRAME.
hide message no-pause.