Advanced PowerBuilder

HomePrevious Lesson: Creating Functions For the Custom User Object
Next Lesson: User Object Level Function uf_initialize

Global Function f_copyrecord

As with all functions, this function is defined in the Function Painter. Invoke the Function painter (Select File > New menu option and double click on Function icon located under Objects tab page). Declare the function, as shown in below and set the return type to Integer.

Name

Type

Pass By

Adw_destination

DataWindow

Ref

Adw_source

DataWindow

Ref

Al_row

Long

Read-Only

Write the following code:
// Global Function: f_CopyRecord()
String ls_ColType
int li_ColCount, li_ColCounter
Long ll_CurrentRow
ll_CurrentRow = adw_destination.InsertRow(0)
li_ColCount = integer( adw_source.Describe( &
              "datawindow.column.count"))
For li_ColCounter = 1 To li_ColCount
   ls_ColType = adw_source.Describe("#" + &
                 String( li_ColCounter ) + ".ColType")
   Choose case upper( left( ls_ColType,5))
      Case "CHAR("
           adw_destination.SetItem( &
                 ll_CurrentRow, li_ColCounter, &
                 adw_source.GetItemString( &
                 al_row, li_ColCounter ))
      Case "NUMBE","DECIM"
           adw_destination.SetItem( &
                 ll_CurrentRow, li_ColCounter, &
                 adw_source.GetItemNumber( &
                 al_row, li_ColCounter ))
      Case "DATE"
           adw_destination.SetItem( &
                 ll_CurrentRow, li_ColCounter, &
                 adw_source.GetItemDate( &
                 al_row, li_ColCounter ))
      Case "DATET"
           adw_destination.SetItem( &
                 ll_CurrentRow, li_ColCounter, &
                 adw_source.GetItemDateTime( &
                 al_row,li_ColCounter ))
      Case "TIME"
           adw_destination.SetItem( &
                 ll_CurrentRow, li_ColCounter, &
                 adw_source.GetItemTime( &
                 al_row, li_ColCounter ))
   End Choose
Next
Return ll_CurrentRow

You can replace the above function with RowsCopy()/RowsMove() DataWindow function, available from version 4.0 onwards. We still wrote this code, to expose you to more DataWindow related programming.

This function inserts the row made up from all the columns within the source DataWindow into the destination DataWindow. The arguments for the Describe() function return the total number of columns to the source DataWindow, while the FOR loop determines the data type of the columns. Finally the CASE statement simply calls the appropriate function, depending on the data type, to get the column value and then copies it to the destination DataWindow.

We use the first five characters of the data type to decide which case statement to execute, as it is the minimum number required to give unique values. If we use less than five, we wouldn't be able to distinguish between DATE and DATETIME.

While writing the code, if you want to change the function declaration, display the prototype and change it (To display the prototype, click on the second icon from right located on the right top corner of the Script Editor view. If the view is pinned, you can find this icon right below the Maximize icon).

We declare the access level as Public so that the function is available to all objects in the application. We don't need to return anything from this function, but we do need to pass three parameters to it, each by value.

You can send any number of parameters to a function, but you have to specify how they are passed; either by value or by reference or by read-only. This completes the definition of f_copyrecord function.
HomePrevious Lesson: Creating Functions For the Custom User Object
Next Lesson: User Object Level Function uf_initialize