Thiru
Thiru

Reputation: 251

How to give an error message if none of the records are matching in FOR EACH loop?

I have written a program for matching one records to another records in FOR Each loop but i don't know how to give an error message if none of the records are matching. Let me share my codes

DEFINE VARIABLE cPos AS INTEGER NO-UNDO.
DEFINE TEMP-TABLE tt_data NO-UNDO
      FIELD cPosition AS CHARACTER FORMAT "X(60)"
      FIELD cEndCode  AS CHARACTER
      FIELD cShotCode AS CHARACTER.
/*so many records are stored in tt_data and below is one of the records for your understanding*/

CREATE tt_data.
ASSIGN
tt_data.cPosition ="S$$$^^^^^^^^^^$$$^^^MC^^^^^^^^^^^^R^^^^^^^^^^^^^^^^^^^^^^^^^"
tt_data.cEndCode =  10
tt_data.cShotCode = "S".

cPos = integer( tt_data.cEndCode / 10 ).

/* Consider 60 records available in tt_data */
FOR EACH tt_data.

     FIND FIRST tt_date WHERE tt_data.cShotCode =  
     SUBSTRING(tt_data.cPosition,cPos,1) NO-LOCK NO-ERROR. 

     DISPLAY tt_data.cShotCode. /* Displayed Value is S */

IF NOT AVAILABLE tt_date THEN
    MESSAGE "NONE OF THE RECORDS MATCHING WITH tt_data.cPosition "
    LEAVE.
END.

I can get at least one record matching in tt_data. Here the problem is I don't want to LEAVE if any one record is matching but I want to get an error message with LEAVE statement if none of the records are matching. Could you please help this case?

Upvotes: 0

Views: 79

Answers (2)

Jeff Grande
Jeff Grande

Reputation: 51

I would personally try to do the error check before you ever get in the FOR EACH block. Sometimes you can't, but based on your sample code I think you could examine the temp-table first and provide an error message. But I'm not completely sure what you are going for based on the sample.

DEFINE VARIABLE cPos AS INTEGER NO-UNDO.
DEFINE TEMP-TABLE tt_data NO-UNDO
      FIELD cPosition AS CHARACTER FORMAT "X(60)"
      FIELD cEndCode  AS CHARACTER
      FIELD cShotCode AS CHARACTER.
/*so many records are stored in tt_data and below is one of the records for your understanding*/

CREATE tt_data.
ASSIGN
tt_data.cPosition ="S$$$^^^^^^^^^^$$$^^^MC^^^^^^^^^^^^R^^^^^^^^^^^^^^^^^^^^^^^^^"
tt_data.cEndCode =  10
tt_data.cShotCode = "S".

cPos = integer( tt_data.cEndCode / 10 ).


/* Add check here */
IF can-find( FIRST tt_date WHERE 
                   tt_data.cShotCode = SUBSTRING(tt_data.cPosition,cPos,1 ) )
THEN
  message "ERROR message".

ELSE DO:
  /* Consider 60 records available in tt_data */
  FOR EACH tt_data.
    /* DO thing */
  END.
END.

Upvotes: 0

Tom Bascom
Tom Bascom

Reputation: 14020

I think this might be what you are trying to do:

DEFINE VARIABLE cPos AS INTEGER NO-UNDO.
DEFINE TEMP-TABLE tt_data NO-UNDO
      FIELD cPosition AS CHARACTER FORMAT "X(60)"
      FIELD cEndCode  AS CHARACTER
      FIELD cShotCode AS CHARACTER.

/*so many records are stored in tt_data and below is one of the records for your understanding*/

CREATE tt_data.
ASSIGN
tt_data.cPosition ="S$$$^^^^^^^^^^$$$^^^MC^^^^^^^^^^^^R^^^^^^^^^^^^^^^^^^^^^^^^^"
tt_data.cEndCode =  10
tt_data.cShotCode = "S".

cPos = integer( tt_data.cEndCode / 10 ).

/* Consider 60 records available in tt_data */

FOR EACH tt_data:  /* although it 'works', "." is not appropriate, FOR EACH should end with a ":" */

    FIND FIRST tt_date WHERE tt_data.cShotCode = SUBSTRING(tt_data.cPosition,cPos,1) NO-LOCK NO-ERROR. 

    /* changes start here */

    IF AVAILABLE tt_date THEN
      do:
        DISPLAY tt_data.cShotCode. /* Displayed Value is S */ /* only display this when it is available! */
      end.
     else
      do:
        MESSAGE "NONE OF THE RECORDS MATCHING WITH tt_data.cPosition ".  /* a "." was missing */
        /* LEAVE. */ 
      end.

END.

Upvotes: 1

Related Questions