Tech Tip: An approach for getting the number of records in a named selection
PRODUCT: 4D | VERSION: 14.1 | PLATFORM: Mac & Win
Published On: September 11, 2014
There is no native command that returns the number of records in a named selection, however it is possible to get this value in code. Here is one approach for getting this value.
First we need to create an error handler to catch a fewerrors that may be found, for example if the named selection does not exist. The error handler is very simple, here it is:
// Method Name: err_RECORDS_IN_NAMED_SELECTION // Description: err handler for RECORDS IN NAMED SELECTION recError:=Error |
Next we need to create the main function that does all the work; here it is:
// Method Name: RECORDS IN NAMED SELECTION // Description: Returns the number of records in the named selection // A return value of -1 represents the named selection does not exist // A return value of -2 represents an invalid table name // A return value of -5 means an error happened for which it is not defined in this code If (Count parameters=2) C_LONGINT($0) // number of records found C_POINTER($1) // pointer to table C_TEXT($2) // name of selection C_LONGINT(recError;undefinedError) C_TEXT($methOnErrCurrent) ARRAY LONGINT($rec_al;0) undefinedError:=-5 recError:=0 $methOnErrCurrent:=Method called on error ON ERR CALL("err_RECORDS_IN_NAMED_SELECTION") LONGINT ARRAY FROM SELECTION($1->;$rec_al;$2) ON ERR CALL($methOnErrCurrent) Case of : (recError=0) // no error $0:=Size of array($rec_al) : (recError=-9977) // -9977 = no named selection matching that name $0:=-1 : (recError=59) // 59 = 4D was expecting a table $0:=-2 Else // some other error that is not yet defined in these case statements // for now we will return -5 but this code can be improved later $0:=undefinedError End case End if |
Finally we have a test method for testing the function:
ALL RECORDS([Table_1]) COPY NAMED SELECTION([Table_1];"SomeNamedSelection") C_LONGINT($t) $t:=RECORDS IN NAMED SELECTION (->[Table_1];"SomeNamedSelection") TRACE |