Understanding IF NOT AVAILABLE STATEMENT

HT3

Member
Hi Guys,

I'm very confused and not sure why my "IF NOT AVAILABLE" statement isn't working under certain scenarios.

For example when I use the FIND function in the working code with the "IF NOT AVAILABLE" statement, the "UNAVAILABLE COLOUR" message is displayed as I expect. I can then amend the code with the following statement "IF AVAILABLE" and message it's an "AVAILABLE COLOUR" else "UNAVAILABLE COLOUR".


WORKING CODE
Code:
DEFINE TEMP-TABLE tt-ColourTab
FIELD ttColour AS CHAR
FIELD ttColCode AS CHAR.

CREATE tt-ColourTab.
tt-ColourTab.ttColour = "RED".
tt-ColourTab.ttColCode = "002".

CREATE tt-ColourTab.
tt-ColourTab.ttColour = "GREEN".
tt-ColourTab.ttColCode = "003".

    FIND tt-ColourTab WHERE
         tt-ColourTab.ttColour = "BLUE".
      
         IF NOT AVAILABLE tt-ColourTab THEN DO:

            MESSAGE "UNAVAILABLE COLOUR".

         END.

         ELSE
      
            MESSAGE "AVAILABLE COLOUR".

However when I use the below blocks of code that aren't working, it always displays the message when the colour exists but doesn't display the required message when it doesn't exist.

NOT WORKING CODE (VERSION 1)
Code:
DEFINE TEMP-TABLE tt-ColourTab
FIELD ttColour AS CHAR
FIELD ttColCode AS CHAR.

CREATE tt-ColourTab.
tt-ColourTab.ttColour = "RED".
tt-ColourTab.ttColCode = "002".

CREATE tt-ColourTab.
tt-ColourTab.ttColour = "GREEN".
tt-ColourTab.ttColCode = "003".

    FOR EACH tt-ColourTab WHERE
             tt-ColourTab.ttColour = "BLUE".
      
         IF NOT AVAILABLE tt-ColourTab THEN DO:

            MESSAGE "UNAVAILABLE COLOUR".

         END.

         ELSE
      
            MESSAGE "AVAILABLE COLOUR".
         
    END.


NOT WORKING CODE (VERSION 2)
Code:
DEFINE TEMP-TABLE tt-ColourTab
FIELD ttColour AS CHAR
FIELD ttColCode AS CHAR.

CREATE tt-ColourTab.
tt-ColourTab.ttColour = "RED".
tt-ColourTab.ttColCode = "002".

CREATE tt-ColourTab.
tt-ColourTab.ttColour = "GREEN".
tt-ColourTab.ttColCode = "003".

 
    FOR EACH tt-ColourTab WHERE
             tt-ColourTab.ttColour = "GREY".
      
         IF AVAILABLE tt-ColourTab THEN DO:

            MESSAGE "AVAILABLE COLOUR".

         END.

         ELSE
      
            MESSAGE "UNAVAILABLE COLOUR".

    END.

I'm obviously missing something but can't see what.... the only difference being FIND vs FOR EACH.

Thanks
 
The FOR EACH block will only show available records. There are cases where this is not the case for joined tables, but for the first table in the query, you will always see records from that first table. If the contents of the FOR EACH block do not execute at least once, then there are no records that satisfy that FOR EACH. But you can't tell that from inside the FOR EACH block.
 
Last edited:
Sorry I'm still not getting it :confused:, I don't satisfy my FOR EACH query but then the very next line I say if it's not available then message me... so should it not display the message?
 
Think of the FOR EACH returning a set. In the above code, the set is empty, so the enclosed code never executes. With the FIND, it is either going to find a record matching the criteria or not, but it will always proceed to the next line, so the not available is reported.

Indeed, were I formatting your code, I would put the FIND and the IF both at the far left, but the FOR EACH at the left and the IF indented.
 
Back
Top