KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Highlight new records in a selection based list box
PRODUCT: 4D | VERSION: 13.0 | PLATFORM: Mac & Win
Published On: August 24, 2012

When adding records from a 4D list form it is normal in a Repeat...Until loop using the command ADD RECORD. Without special intervention the result of the ADD RECORD once back in the list form is there is one record in selection in the list.

Often, the desire has been to have a selection of records in the list form of those records that have just been added to the table. Going even a step further is to have the selection of records that were in the list to be preserved with the newly added records added to the selection. The code below accomplishes that task.

The code below is targeted at list forms that use a 4D List Box, but it can also be used in 4D standard list (output) forms — just be sure to use "User set" instead of the list box highlight set.

This code snippet also preserves the sort order of the existing selection with the new records being highlighted at the bottom of the list.

ARRAY LONGINT($Selection_aL;0)
LONGINT ARRAY FROM SELECTION(LSTFRM_Table_P->;$Selection_aL)

  // Use a new empty set incase the highlight set
  // already had some records in it.
  // (
CREATE EMPTY SET(LSTFRM_Table_P->;"$Add_Set")
  // )
Repeat
    ADD RECORD(LSTFRM_Table_P->)
    If (OK=1)
       ADD TO SET(LSTFRM_Table_P->;"$Add_Set")
   
       $RIS:=Record number(LSTFRM_Table_P->)
       APPEND TO ARRAY($Selection_aL;$RIS)
    End if
Until (OK=0)

  // Put the new records into the highlight set
  // (
COPY SET("$Add_Set";"$LSTFRM_Set")
CLEAR SET("$Add_Set")
  // )

  // Create a selection of records that contains the previous
  // selection plus the new records
  // (
CREATE SELECTION FROM ARRAY(LSTFRM_Table_P->;$Selection_aL)
  // )