Function Transfer_EditTable_Data(SourceEditTable, TargetEditTable, ArrayFlag, UpdateCellEdit)

/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from SRP Computer Solutions, Inc.

    Name        :   Transfer_EditTable_Data

    Description :   Transfer data from one EditTable to another. Normally this will be from an OpenInsight databound
                    EditTable to an SRP OLE EditTable and vice-versa.

    Notes       :   This routine will typically be called from an OLE EditTable's LostFocus event and from the window's
                    WRITE_PRE and READ_POST events. A check is first made to see if the controls are valid. If not, the
                    function will return a failure flag.

    Parameters  :
        SourceEditTable     [in] -- The EditTable with the data that needs to be transferred.
        TargetEditTable     [in] -- The EditTable that the data will be transferred in to.
        ArrayFlag           [in] -- If the target is an OpenInsight EditTable then this flag indicates whether the ARRAY
                                    property instead of the DEFPROP property should property should not be set after the
                                    EditTable is populated.
        UpdateCellEdit      [in] -- Flag to determine if the UpdateCellEdit method should be called before the
                                    transfer occurs. Normally only done during a window's WRITE event to make sure all
                                    in-progress changes are updated to the SRP EditTable control. If this is set during
                                    an SRP EditTable event this could cause the focus to get stuck in the control.
        Success            [out] -- Return flag to indicate if the process was successful.

    History     :   (Date, Initials, Notes)
        01/22/06    dmb     Original programmer
        02/16/08    dmb     Add UpdateCellEdit parameter to determine if the UpdateCellEdit method should be called
        07/21/08    rch     Set Redraw off and on for Target OLE EditTable
        07/14/10    rch     Clear out databound edit table columns in @RECORD to work around OI problems
        07/20/10    rch     Skip copying of source data to target table if both have the same data

***********************************************************************************************************************/

$insert LOGICAL

Declare function   Get_Property
Declare subroutine Set_Property, Send_Message

Success = Yes$  ;   // Assume successful for now.
If Assigned(SourceEditTable) else SourceEditTable = ''
If Assigned(TargetEditTable) else TargetEditTable = ''
If Assigned(ArrayFlag)       else ArrayFlag = No$
If ArrayFlag EQ ''           then ArrayFlag = No$
If Assigned(UpdateCellEdit)  else UpdateCellEdit = No$
If UpdateCellEdit EQ ''      then UpdateCellEdit = No$

If Get_Property(SourceEditTable, 'HANDLE') then
    If Get_Property(TargetEditTable, 'HANDLE') then
        If Get_Property(SourceEditTable, 'OLE.ProgID') EQ 'SRP.EditTable.1' then
            SourceProp = 'OLE.Array'
            If UpdateCellEdit then Send_Message(SourceEditTable, 'OLE.UpdateCellEdit')
        end else
            If ArrayFlag EQ Yes$ then
                SourceProp = 'ARRAY'
            end else
                SourceProp = 'DEFPROP'
            end
        end
        If Get_Property(TargetEditTable, 'OLE.ProgID') EQ 'SRP.EditTable.1' then
            TargetProp = 'OLE.Array'
            If UpdateCellEdit then Send_Message(TargetEditTable, 'OLE.UpdateCellEdit')
        end else
            If ArrayFlag EQ Yes$ then
                TargetProp = 'ARRAY'
            end else
                TargetProp = 'DEFPROP'
            end
        end
    end else
        Success = No$
    end
end else
    Success = No$
end

If Success then
    *Data = Get_Property(SourceEditTable, SourceProp)
    SourceData = Get_Property(SourceEditTable, SourceProp)
    TargetData = Get_Property(TargetEditTable, TargetProp)
    If SourceData NE TargetData then
        // Skip this if source and target have the same data & avoid SAVEWARN message 
        If TargetProp NE 'OLE.Array' then
            // The target EditTable is an OpenInsight control.
            
            // Clear the @RECORD position values before setting its property. 
            // Otherwise, an OI bug may result in incorrect values in some {FIELD} references when symbolics are calculated.
            PosValues = Get_Property(TargetEditTable, 'POS')
            If PosValues then
                NumValues = Count(PosValues, @SVM) + (PosValues NE '')
                For iVal  = 1 to NumValues
                    Pos = PosValues<0, 0, iVal>
                    If Pos then
                        @RECORD<Pos> = ''
                    end
                Next iVal
                Set_Property(@Window, 'RECORD', @RECORD)
            end
            
            // Make sure there are enough empty rows before setting its property. 
            // Otherwise, the data might get truncated.
            *NumDataRows = DCount(Data<1>, @VM)
            NumDataRows = DCount(SourceData<1>, @VM)
            Array = Get_Property(TargetEditTable, TargetProp)
            NumEditTableRows = DCount(Array<1>, @VM)
            If NumDataRows GT NumEditTableRows then
                NumExtraRows = NumDataRows - NumEditTableRows
                For i = 1 to NumExtraRows
                    Send_Message(TargetEditTable, 'INSERT', -1, '')
                Next i
            end
        end else
            // Set Redraw to false for Target OLE EditTable control
            Set_Property(TargetEditTable, 'OLE.Redraw', False$)
        end
        
        *Set_Property(TargetEditTable, TargetProp, Data)
        Set_Property(TargetEditTable, TargetProp, SourceData)
        
        If TargetProp EQ 'OLE.Array' then
            Set_Property(TargetEditTable, 'OLE.Redraw', True$)
        end
    end  
end

Return Success
